Beispiel #1
0
        public async Task <IResultModel> BindRole(AccountRoleBindModel model)
        {
            var exists = await _accountRepository.ExistsAsync(model.AccountId);

            if (!exists)
            {
                return(ResultModel.Failed("账户不存在"));
            }

            exists = await _roleRepository.ExistsAsync(model.RoleId);

            if (!exists)
            {
                return(ResultModel.Failed("角色不存在"));
            }

            //添加
            if (model.Checked)
            {
                exists = await _accountRoleRepository.Exists(model.AccountId, model.RoleId);

                if (!exists)
                {
                    var result = await _accountRoleRepository.AddAsync(new AccountRoleEntity { AccountId = model.AccountId, RoleId = model.RoleId });

                    return(ResultModel.Result(result));
                }

                return(ResultModel.Success());
            }
            {
                //删除
                var result = await _accountRoleRepository.Delete(model.AccountId, model.RoleId);

                return(ResultModel.Result(result));
            }
        }
Beispiel #2
0
        public async Task <ICreateUserResponse> CreateAccountAsync(IAccount account, params IRole[] accountRoles)
        {
            var existingUser = await _accountRepository.FindByEmailAsync(account.Email);

            if (existingUser != null)
            {
                return(new CreateUserResponse(false, "Email already in use.", null));
            }

            account.Password = _passwordHandler.HashPassword(account.Password);

            await _accountRepository.CreateAsync(account, EntityTypesEnum.USER);

            await _accountRoleRepository.AddAsync(account, accountRoles);

            await _unitOfWork.CompleteAsync();

            return(new CreateUserResponse(true, null, account));
        }
Beispiel #3
0
        public async Task <IResultModel> Install(SystemInstallModel model)
        {
            await _moduleInfoService.Sync();

            await _permissionService.Sync(model.Permissions);

            var role = new RoleEntity
            {
                Name = "系统管理员"
            };

            await _roleRepository.AddAsync(role);

            var account = new AccountEntity
            {
                UserName = "******",
                Password = "******",
                Name     = "管理员"
            };
            await _accountRepository.AddAsync(account);

            await _accountRoleRepository.AddAsync(new AccountRoleEntity { AccountId = account.Id, RoleId = role.Id });

            UpdateConfig(new SystemConfigModel
            {
                Title    = "腾迪权限管理系统",
                Auditing = false,
                Toolbar  = new SystemToolbar
                {
                    Fullscreen = true,
                    Skin       = true,
                    Logout     = true,
                    UserInfo   = true
                }
            });

            return(ResultModel.Success());
        }
Beispiel #4
0
        public async Task <IResultModel <Guid> > Add(AccountAddModel model, IUnitOfWork uow = null)
        {
            var result = new ResultModel <Guid>();

            var account = _mapper.Map <AccountEntity>(model);

            var exists = await Exists(account);

            if (!exists.Successful)
            {
                return(exists);
            }

            //默认未激活状态,用户首次登录激活
            account.Status = AccountStatus.Inactive;

            //设置默认密码
            if (account.Password.IsNull())
            {
                if (_options != null && _options.DefaultPassword.NotNull())
                {
                    account.Password = _options.DefaultPassword;
                }
                else
                {
                    account.Password = "******";
                }
            }

            account.Password = _passwordHandler.Encrypt(account.UserName, account.Password);

            //如果uow参数为空,需要自动处理工作单元
            var noUow = uow == null;

            if (noUow)
            {
                uow = _dbContext.NewUnitOfWork();
            }

            if (await _accountRepository.AddAsync(account, uow))
            {
                if (model.Roles != null && model.Roles.Any())
                {
                    var accountRoleList = model.Roles.Select(m => new AccountRoleEntity {
                        AccountId = account.Id, RoleId = m
                    }).ToList();
                    if (await _accountRoleRepository.AddAsync(accountRoleList, uow))
                    {
                        if (noUow)
                        {
                            uow.Commit();
                        }

                        return(result.Success(account.Id));
                    }
                }
                else
                {
                    if (noUow)
                    {
                        uow.Commit();
                    }

                    return(result.Success(account.Id));
                }
            }

            return(result.Failed());
        }
        public override async Task <int> HandleCommand(UpdateAccountCommand request, CancellationToken cancellationToken)
        {
            AccountSingleRole account = null;

            if (request.Model == null || request.Model.Id == 0)
            {
                throw new BusinessException("Account.NotExisted");
            }
            else
            {
                account = await accountQueries.GetByIdAsync(request.Model.Id);

                if (account == null)
                {
                    throw new BusinessException("Account.NotExisted");
                }
            }

            if ((await roleQueries.GetByIdAsync(request.Model.RoleId ?? 0)) == null)
            {
                throw new BusinessException("Role.NotSelected");
            }

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        if (account.RoleId != request.Model.RoleId)
                        {
                            var accRoles = (await accountRoleQueries.GetsAsync($"user_account_id = {request.Model.Id}")).ToList();
                            for (int i = 0; i < accRoles.Count; i++)
                            {
                                await accountRoleRepository.DeleteAsync(accRoles[i].Id);
                            }

                            await accountRoleRepository.AddAsync(new AccountRole()
                            {
                                RoleId        = request.Model.RoleId ?? 0,
                                UserAccountId = account.Id,
                                CreatedDate   = DateTime.Now,
                            });
                        }

                        request.Model.ModifiedDate     = DateTime.Now;
                        request.Model.ModifiedBy       = request.LoginSession.Id;
                        request.Model.IsDeleted        = account.IsDeleted;
                        request.Model.SecurityPassword = account.SecurityPassword;
                        if (!string.IsNullOrWhiteSpace(request.Model.NewPassword))
                        {
                            request.Model.Password = (request.Model.NewPassword + request.Model.SecurityPassword.ToString()).CalculateMD5Hash();
                        }
                        else
                        {
                            request.Model.Password = account.Password;
                        }

                        if (await accountRepository.UpdateAsync((Account)request.Model) > 0)
                        {
                            rs = 0;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try { trans.Rollback(); } catch { }
                        }
                    }
                }
            }
            return(rs);
        }
        public override async Task <int> HandleCommand(InsertAccountPartnerCommand request, CancellationToken cancellationToken)
        {
            var company = await _companyQueries.GetByUserIdAsync(request.LoginSession.Id);

            if (company == null)
            {
                throw new BusinessException("Common.NoPermission");
            }

            var id = 0;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        request.Model.RoleId = (await _settingQueries.GetValueAsync(SettingKeys.Role_Default)).ToInt();
                        if ((await _roleQueries.GetByIdAsync(request.Model.RoleId ?? 0)) == null)
                        {
                            throw new BusinessException("Role.NotSelected");
                        }

                        request.Model.PartnerId        = company.Id;
                        request.Model.CreatedDate      = DateTime.Now;
                        request.Model.CreatedBy        = request.LoginSession.Id;
                        request.Model.ModifiedDate     = DateTime.Now;
                        request.Model.ModifiedBy       = request.LoginSession.Id;
                        request.Model.SecurityPassword = Guid.NewGuid();
                        request.Model.Password         = (request.Model.NewPassword + request.Model.SecurityPassword.ToString()).CalculateMD5Hash();

                        id = await _accountRepository.AddAsync((Account)request.Model);

                        if (id > 0)
                        {
                            await _accountRoleRepository.AddAsync(new AccountRole()
                            {
                                RoleId        = request.Model.RoleId ?? 0,
                                UserAccountId = id,
                                CreatedDate   = DateTime.Now,
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (id > 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try { trans.Rollback(); } catch { }
                        }
                    }
                }
            }

            return(id);
        }