Example #1
0
 public async Task <ApiResult> Gets(int roleId)
 {
     return(new ApiResult()
     {
         Result = 0,
         Data = await roleQueries.Get(roleId)
     });
 }
        public override async Task <int> HandleCommand(DeleteCommand request, CancellationToken cancellationToken)
        {
            var role = await roleQueries.Get(request.RoleId);

            if (role != null && !role.IsDeleted)
            {
                role = DeleteBuild(role, request.LoginSession);
                return(await roleRepository.Update(role));
            }
            return(0);
        }
        public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken)
        {
            var role = await roleQueries.Get(request.RoleId);

            if (role == null)
            {
                throw new BusinessException("User.NotExsitedRole");
            }

            foreach (var item in request.Permissions)
            {
                item.RoleId = request.RoleId;
                await rolePermissionRepository.AddOrUpdate(item);
            }

            return(0);
        }
Example #4
0
        public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken)
        {
            if (request.Role == null || request.Role.Id == 0)
            {
                throw new BusinessException("Common.WrongInput");
            }
            var role = await roleQueries.Get(request.Role.Id);

            if (role == null)
            {
                throw new BusinessException("User.NotExistedRole");
            }

            var checkingRole = await roleQueries.GetByName(request.Role.Name);

            if (checkingRole != null && checkingRole.Id != request.Role.Id)
            {
                throw new BusinessException("User.ExistedRole");
            }

            var rs = -1;

            using (var conn = DalHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        roleRepository.JoinTransaction(conn, trans);
                        rolePermissionRepository.JoinTransaction(conn, trans);

                        role.Name        = request.Role.Name;
                        role.Description = request.Role.Description;
                        role.IsActive    = request.Role.IsActive;
                        role             = UpdateBuild(role, request.LoginSession);
                        var rsUp = await roleRepository.Update(role);

                        if (rsUp == -1)
                        {
                            return(rs);
                        }

                        foreach (var item in request.Role.RolePermissions)
                        {
                            item.RoleId = role.Id;
                            await rolePermissionRepository.AddOrUpdate(item);
                        }

                        rs = 0;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch (Exception ex)
                            {
                                LogHelper.GetLogger().Error(ex);
                            }
                        }
                    }
                }
            }

            return(rs);
        }
Example #5
0
        public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken)
        {
            // Role is null that this action is made by the staff. But LoginSession must not null
            if (request.LoginSession == null && request.RoleId == 0)
            {
                throw new NotPermissionException();
            }

            //check existed username
            var user = await userQueries.Get(request.UserName);

            if (user != null)
            {
                throw new BusinessException("ExistedAccount");
            }

            //check existed email
            user = await userQueries.GetWithEmail(request.Email);

            if (user != null)
            {
                throw new BusinessException("ExistedEmail");
            }

            //Add user
            Guid securityPassword = Guid.NewGuid();
            var  userId           = await this.userRepository.Add(new UserAccount()
            {
                Username         = request.UserName,
                Password         = (request.Password.Trim() + securityPassword.ToString()).CalculateMD5Hash(),
                Email            = request.Email,
                DisplayName      = request.DisplayName,
                PhoneNumber      = request.PhoneNumber,
                SecurityPassword = securityPassword,
                IsExternalUser   = request.RoleId != 0,
                IsActived        = request.RoleId == 0,
                IsUsed           = request.RoleId != 0 || request.IsUsed,
                CreatedDate      = DateTime.Now
            }, request.LoginSession == null?null : (int?)request.LoginSession.UserId);

            // for who is't the staff
            if (userId > 0 && request.RoleId != 0)
            {
                var role = await roleQueries.Get(request.RoleId);

                if (role != null && role.IsExternalRole)
                {
                    await userRoleRepository.Add(new UserAccountRole()
                    {
                        UserId = userId,
                        RoleId = role.Id
                    });
                }
                else
                {
                    throw new BusinessException("Role.NotExisted");
                }
            }

            //Send email for active here


            return(userId);
        }
        public override async Task <int> HandleCommand(ChangeRolesCommand request, CancellationToken cancellationToken)
        {
            var user = await userQueries.Get(request.UserId);

            if (user == null)
            {
                throw new BusinessException("NotExistedAccount");
            }
            if (user.IsExternalUser)
            {
                throw new BusinessException("ExternalAccount");
            }
            var rs = -1;

            using (var conn = DalHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        userRoleRepository.JoinTransaction(conn, trans);
                        roleQueries.JoinTransaction(conn, trans);
                        await userRoleRepository.DeleteRole(user.Id);

                        foreach (var item in request.RoleIds)
                        {
                            var role = await roleQueries.Get(item);

                            if (role != null && role.IsActive && !role.IsExternalRole && role.Id != 1) // != Administrator
                            {
                                await userRoleRepository.Add(new UI.Models.UserAccountRole()
                                {
                                    RoleId = role.Id,
                                    UserId = user.Id
                                });
                            }
                        }
                        return(rs = 0);
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch (Exception ex)
                            {
                                LogHelper.GetLogger().Error(ex);
                            }
                        }
                    }
                }
            }
        }