コード例 #1
0
ファイル: UserService.cs プロジェクト: balbarak/password-box
        private async Task UpdateRolesAsync(string userId, string[] roles, PasswordBoxUserManager manager = null)
        {
            if (manager == null)
            {
                manager = PasswordBoxUserManager.Create();
            }

            var user = await manager.FindByIdAsync(userId);

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var userRoles = await manager.GetRolesAsync(user);

            if (userRoles != null && userRoles.Count > 0)
            {
                await manager.RemoveFromRolesAsync(user, userRoles);
            }

            if (roles != null)
            {
                await manager.AddToRolesAsync(user, roles);
            }
        }
コード例 #2
0
ファイル: UserService.cs プロジェクト: balbarak/password-box
        public async Task <User> GetByID(string id)
        {
            User result = null;

            using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create())
            {
                result = await manager.FindByIdAsync(id);
            }

            return(result);
        }
コード例 #3
0
ファイル: UserService.cs プロジェクト: balbarak/password-box
        public async Task DeleteAsync(string id)
        {
            using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create())
            {
                var user = await manager.FindByIdAsync(id);

                if (user != null)
                {
                    var result = await manager.DeleteAsync(user);

                    if (!result.Succeeded)
                    {
                        throw new BusinessException(result.Errors.Select(a => a.Description).ToList());
                    }
                }
            }
        }
コード例 #4
0
ファイル: UserService.cs プロジェクト: balbarak/password-box
        public async Task <User> UpdateAsync(User entity, string[] roles = null)
        {
            using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create())
            {
                var user = await manager.FindByIdAsync(entity.Id);

                user = user.Update(entity);

                var result = await manager.UpdateAsync(user);

                if (!result.Succeeded)
                {
                    throw new BusinessException(result.Errors.Select(a => a.Description).ToList());
                }

                await UpdateRolesAsync(entity.Id, roles, manager);
            }


            return(entity);
        }
コード例 #5
0
        public async Task <List <string> > GetRolesByUserIdAsync(string id)
        {
            if (String.IsNullOrWhiteSpace(id))
            {
                return(new List <string>());
            }

            List <string> result = new List <string>();

            using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create())
            {
                var user = await manager.FindByIdAsync(id);

                if (user != null)
                {
                    var roles = await manager.GetRolesAsync(user);

                    result = roles.ToList();
                }
            }

            return(result);
        }
コード例 #6
0
ファイル: UserService.cs プロジェクト: balbarak/password-box
        public async Task <User> Add(User entity, string password, string[] roles = null)
        {
            if (String.IsNullOrWhiteSpace(password))
            {
                throw new BusinessException(MessageText.PleaseWritePassword);
            }

            using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create())
            {
                IdentityResult result = await manager.CreateAsync(entity, password);

                if (!result.Succeeded)
                {
                    throw new BusinessException(result.Errors.Select(a => a.Description).ToList());
                }

                if (roles != null)
                {
                    await manager.AddToRolesAsync(entity, roles);
                }
            }

            return(entity);
        }