public async Task <IActionResult> ChangeRole([FromBody] Roles roles)
        {
            string role = await _appDbContext.GetRoleByToken(Request.Headers["Authorization"]);

            if (role.ToLower() == "admin")
            {
                _log.LogInfo("Get new role.");
                if (!ModelState.IsValid)
                {
                    _log.LogError("Incorrect input.");
                    return(BadRequest(ModelState));
                }

                if (await _appDbContext.ChangeRole(roles) == null)
                {
                    _log.LogError("Incorrect role.");
                    return(BadRequest(Errors.AddErrorToModelState("rolesFailure", "Invalid role.", ModelState)));
                }

                _log.LogInfo("Changing role.");
                await _appDbContext.ChangeRole(roles);

                string roleChangeText;
                using (StreamReader streamreader = new StreamReader(@"..\EHospital.Authorization.WebAPI\Letters\roleChange.txt"))
                {
                    roleChangeText = streamreader.ReadToEnd();
                }

                string email = await _appDbContext.GetEmail(roles);

                await emailSender.SendEmail(email, "Free access", roleChangeText);

                return(Ok());
            }

            return(BadRequest(role));
        }
Beispiel #2
0
        public Task <string> GetRoleByToken()
        {
            var headers = Request.Headers;
            var token   = "";

            foreach (var head in headers)
            {
                if (head.Key == "Authorization")
                {
                    token = head.Value;
                    break;
                }
            }

            return(_userDataProvider.GetRoleByToken(token));
        }