예제 #1
0
        public async Task <GenericResult> DeleteAsync(Guid id)
        {
            var appRole = await _roleManager.FindByIdAsync(id.ToString());

            if (appRole != null)
            {
                // Remove all Users from Role before deleting role
                var UsersInRole = await _userManager.GetUsersInRoleAsync(appRole.Name);

                foreach (var user in UsersInRole)
                {
                    await _userManager.RemoveFromRoleAsync(user, appRole.Name);
                }
                // Remove Role
                var isDeleted = await _roleManager.DeleteAsync(appRole);

                if (isDeleted.Succeeded)
                {
                    return(new GenericResult(true, appRole.Name));
                }
                else
                {
                    return(new GenericResult(false, ConcenateErrors.Error(isDeleted.Errors)));
                }
            }
            return(new GenericResult(false, CommonConstants.notFound));
        }
예제 #2
0
        public async Task <GenericResult> UpdateAsync(AppUserViewModel appUserVm)
        {
            var appUser = await _userManager.FindByIdAsync(appUserVm.Id.ToString());

            if (appUser != null)
            {
                if (appUserVm.Roles.Count > 0)
                {
                    var roles = await _userManager.GetRolesAsync(appUser);

                    var isDeleted = await _userManager.RemoveFromRolesAsync(appUser, roles);

                    if (isDeleted.Succeeded == false)
                    {
                        var errors = ConcenateErrors.Error(isDeleted.Errors);
                        return(new GenericResult(errors));
                    }
                    var isAdded = await _userManager.AddToRolesAsync(appUser, appUserVm.Roles);

                    if (isAdded.Succeeded == false)
                    {
                        var errors = ConcenateErrors.Error(isDeleted.Errors);
                        return(new GenericResult(errors));
                    }
                }
                else
                {
                    var roles = await _userManager.GetRolesAsync(appUser);

                    var isDeleted = await _userManager.RemoveFromRolesAsync(appUser, roles);

                    if (isDeleted.Succeeded == false)
                    {
                        var errors = ConcenateErrors.Error(isDeleted.Errors);
                        return(new GenericResult(errors));
                    }
                }
                appUser.Email        = appUserVm.Email;
                appUser.PhoneNumber  = appUserVm.PhoneNumber;
                appUser.FullName     = appUserVm.FullName;
                appUser.Balance      = appUserVm.Balance;
                appUser.Avatar       = appUserVm.Avatar;
                appUser.Status       = appUserVm.Status;
                appUser.ModifiedDate = DateTime.Now;

                var result = await _userManager.UpdateAsync(appUser);

                if (result.Succeeded)
                {
                    return(new GenericResult(true, appUser.UserName));
                }
                else
                {
                    var errors = ConcenateErrors.Error(result.Errors);
                    return(new GenericResult(false, errors));
                }
            }
            return(new GenericResult(false));
        }
예제 #3
0
        public async Task <GenericResult> AddAsync(AppRoleViewModel appRoleVm)
        {
            var appRole = new AppRole(appRoleVm.Name, appRoleVm.Description);
            var isAdded = await _roleManager.CreateAsync(appRole);

            if (isAdded.Succeeded)
            {
                return(new GenericResult(true, appRole.Name));
            }
            else
            {
                return(new GenericResult(false, ConcenateErrors.Error(isAdded.Errors)));
            }
        }
예제 #4
0
        public async Task <GenericResult> AddAsync(AppUserViewModel appUserVm)
        {
            var user = new AppUser()
            {
                UserName    = appUserVm.UserName,
                Email       = appUserVm.Email,
                Avatar      = appUserVm.Avatar,
                Balance     = appUserVm.Balance,
                CreatedDate = DateTime.Now,
                FullName    = appUserVm.FullName,
                PhoneNumber = appUserVm.PhoneNumber,
                Status      = appUserVm.Status,
            };
            var result = await _userManager.CreateAsync(user, appUserVm.Password);

            if (result.Succeeded)
            {
                var isUserExisted = await _userManager.FindByNameAsync(user.UserName);

                if (isUserExisted != null)
                {
                    if (appUserVm.Roles.Count > 0)
                    {
                        var isSuccess = await _userManager.AddToRolesAsync(user, appUserVm.Roles);

                        if (isSuccess.Succeeded)
                        {
                            return(new GenericResult(user.UserName));
                        }
                        else
                        {
                            var errors = ConcenateErrors.Error(result.Errors);
                            return(new GenericResult(errors));
                        }
                    }
                }
                else
                {
                    return(new GenericResult(false));
                }
                return(new GenericResult(user.UserName));
            }
            else
            {
                var errors = ConcenateErrors.Error(result.Errors);
                return(new GenericResult(errors));
            }
        }
예제 #5
0
        public async Task <GenericResult> UpdateAsync(AppRoleViewModel appRoleVm)
        {
            var appRole = await _roleManager.FindByIdAsync(appRoleVm.Id.ToString());

            if (appRoleVm != null)
            {
                appRole.Name        = appRoleVm.Name;
                appRole.Description = appRoleVm.Description;

                var isUpdated = await _roleManager.UpdateAsync(appRole);

                if (isUpdated.Succeeded)
                {
                    return(new GenericResult(true, appRole.Name));
                }
                else
                {
                    return(new GenericResult(false, ConcenateErrors.Error(isUpdated.Errors)));
                }
            }
            return(new GenericResult(false, CommonConstants.notFound));
        }