public async Task <IActionResult> CreateRole([FromBody] RoleModel model)
        {
            IdentityRole identityRole = new IdentityRole()
            {
                Name = model.RoleName
            };

            IdentityResult result = await roleManager.CreateAsync(identityRole);

            if (result.Succeeded)
            {
                return(Ok(new ReponseStatus {
                    Status = "Success", Message = "New Role Created"
                }));
            }

            ReponseStatus respStatus = new ReponseStatus();

            foreach (IdentityError error in result.Errors)
            {
                respStatus.Message = respStatus.Message + error.Description + "\n";
            }

            return(BadRequest(respStatus));
        }
        public async Task <IActionResult> AssignRole([FromBody] UserToRoleModel model)
        {
            var user = await userManager.FindByIdAsync(model.UserId);

            var role = await roleManager.FindByIdAsync(model.RoleId);

            if (await userManager.IsInRoleAsync(user, role.Name))
            {
                return(BadRequest(new ReponseStatus {
                    Status = "Failure", Message = "User already has the Role"
                }));
            }


            IdentityResult result = await userManager.AddToRoleAsync(user, role.Name);

            if (result.Succeeded)
            {
                return(Ok(new ReponseStatus {
                    Status = "Success", Message = role.Name + " assigned to User"
                }));
            }

            ReponseStatus respStatus = new ReponseStatus();

            foreach (IdentityError error in result.Errors)
            {
                respStatus.Message = respStatus.Message + error.Description + "\n";
            }

            return(BadRequest(respStatus));
        }
Пример #3
0
        public async Task <IActionResult> Register([FromBody] RegisterModel model)
        {
            //check if email is already in use if yes.. throw error

            var useremail = await userManager.FindByEmailAsync(model.Email);

            if (useremail != null)
            {
                return(NotFound(new ReponseStatus {
                    Status = "Failure", Message = "Email is already taken"
                }));
            }

            MailAddress mailAddress = new MailAddress(model.Email);

            //MODIFY HOST DOMAIN NAME HERE => CURRENTLY only GMAIL and MAILINATOR
            if ((mailAddress.Host.ToUpper() != "MAILINATOR.COM") && mailAddress.Host.ToUpper() != "GMAIL.COM")
            {
                return(NotFound(new ReponseStatus {
                    Status = "Failure", Message = "Use company mail address!"
                }));
            }
            //Creating a IdentityUser object
            var user = new ApplicationUser {
                UserName = model.Username, Email = model.Email, PasswordHash = model.Password
            };

            IdentityResult result = await userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                return(Ok(new ReponseStatus {
                    Status = "Success", Message = "User Registered Successfully"
                }));
            }

            ReponseStatus respStatus = new ReponseStatus();

            foreach (IdentityError error in result.Errors)
            {
                respStatus.Message = respStatus.Message + error.Description + "\n";
            }

            return(BadRequest(respStatus));
        }
        public async Task <IActionResult> DeleteRole(string id)
        {
            var role = await roleManager.FindByIdAsync(id);

            IdentityResult result = await roleManager.DeleteAsync(role);

            if (result.Succeeded)
            {
                return(Ok(new ReponseStatus {
                    Status = "Success", Message = "Role Deleted"
                }));
            }

            ReponseStatus respStatus = new ReponseStatus();

            foreach (IdentityError error in result.Errors)
            {
                respStatus.Message = respStatus.Message + error.Description + "\n";
            }

            return(NotFound(respStatus));
        }
        public async Task <IActionResult> EditUser(EditUserModel model)
        {
            var user = await userManager.FindByIdAsync(model.Id);

            user.UserName = model.Username;

            IdentityResult result = await userManager.UpdateAsync(user);

            if (result.Succeeded)
            {
                return(Ok(new ReponseStatus {
                    Status = "Success", Message = "User Updated"
                }));
            }

            ReponseStatus respStatus = new ReponseStatus();

            foreach (IdentityError error in result.Errors)
            {
                respStatus.Message = respStatus.Message + error.Description + "\n";
            }

            return(NotFound(respStatus));
        }
        public async Task <IActionResult> EditRole(EditRoleModel model)
        {
            var role = await roleManager.FindByIdAsync(model.Id);

            role.Name = model.RoleName;

            IdentityResult result = await roleManager.UpdateAsync(role);

            if (result.Succeeded)
            {
                return(Ok(new ReponseStatus {
                    Status = "Success", Message = "Role Updated"
                }));
            }

            ReponseStatus respStatus = new ReponseStatus();

            foreach (IdentityError error in result.Errors)
            {
                respStatus.Message = respStatus.Message + error.Description + "\n";
            }

            return(NotFound(respStatus));
        }