public async Task UpdateAsync(UpdateAdminUserDTO dto)
        {
            using (AdminUserContext ctx = new AdminUserContext())
            {
                BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx);
                var phoneAdminUser = await bs.GetAll().SingleOrDefaultAsync(e => e.PhoneNum == dto.PhoneNum);

                if (phoneAdminUser != null)
                {
                    if (phoneAdminUser.Id != dto.Id)
                    {
                        throw new Exception("电话号码已存在");
                    }
                }

                AdminUserEntity entity = await bs.GetAll().SingleOrDefaultAsync(e => e.Id == dto.Id);

                if (entity == null)
                {
                    throw new ArgumentException($"管理员{dto.Id}不存在");
                }
                entity.Age      = dto.Age;
                entity.Gender   = dto.Gender;
                entity.Name     = dto.Name;
                entity.PhoneNum = dto.PhoneNum;
                await ctx.SaveChangesAsync();
            }
        }
Ejemplo n.º 2
0
        public async Task UpdateAdminUserToRolesAsync(long adminUserId, long[] roleIds)
        {
            using (AdminUserContext ctx = new AdminUserContext())
            {
                BaseService <AdminUserEntity> adminuserBs = new BaseService <AdminUserEntity>(ctx);
                var adminuser = await adminuserBs.GetAll().SingleAsync(e => e.Id == adminUserId);

                BaseService <AdminUserRoleEntity> adminuserRoleBs = new BaseService <AdminUserRoleEntity>(ctx);
                await adminuserRoleBs.GetAll().ForEachAsync(e =>
                {
                    if (e.AdminUserId == adminUserId)
                    {
                        ctx.AdminUserRoles.Remove(e);
                    }
                });

                BaseService <RoleEntity> roleBs = new BaseService <RoleEntity>(ctx);
                await roleBs.GetAll().ForEachAsync(e =>
                {
                    if (roleIds.Any(x => x == e.Id))
                    {
                        AdminUserRoleEntity en = new AdminUserRoleEntity();
                        en.Role      = e;
                        en.AdminUser = adminuser;
                        ctx.AdminUserRoles.Add(en);
                    }
                });

                await ctx.SaveChangesAsync();
            }
        }
        public async Task <long> AddNewAsync(AddAdminUserDTO dto)
        {
            AdminUserEntity entity = new AdminUserEntity();

            entity.Age          = dto.Age;
            entity.Gender       = dto.Gender;
            entity.Name         = dto.Name;
            entity.PhoneNum     = dto.PhoneNum;
            entity.Salt         = MD5Helper.GetSalt(10);
            entity.PasswordHash = MD5Helper.CalcMD5(dto.Password + entity.Salt);
            using (AdminUserContext ctx = new AdminUserContext())
            {
                BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx);
                var phoneAdminUser = await bs.GetAll().SingleOrDefaultAsync(e => e.PhoneNum == dto.PhoneNum);

                if (phoneAdminUser != null)
                {
                    throw new Exception("电弧号码已存在");
                }
                await ctx.AdminUsers.AddAsync(entity);

                await ctx.SaveChangesAsync();

                return(entity.Id);
            }
        }
        public async Task UpdateRoleToPermissesAsync(long roleId, long[] PermissionIds)
        {
            using (AdminUserContext ctx = new AdminUserContext())
            {
                BaseService <RoleEntity> roleBs = new BaseService <RoleEntity>(ctx);
                var role = await roleBs.GetAll().SingleAsync(e => e.Id == roleId);

                BaseService <RolePermissionEntity> rolePermissionBs = new BaseService <RolePermissionEntity>(ctx);
                await rolePermissionBs.GetAll().ForEachAsync(e =>
                {
                    if (e.RoleId == roleId)
                    {
                        ctx.RolePermission.Remove(e);
                    }
                });

                BaseService <PermissionEntity> permissionBs = new BaseService <PermissionEntity>(ctx);
                await permissionBs.GetAll().ForEachAsync(e =>
                {
                    if (PermissionIds.Any(x => x == e.Id))
                    {
                        RolePermissionEntity en = new RolePermissionEntity();
                        en.Role       = role;
                        en.Permission = e;
                        ctx.RolePermission.Add(en);
                    }
                });

                await ctx.SaveChangesAsync();
            }
        }
        public async Task EditorPasswordAsync(long id, string newPassword)
        {
            using (AdminUserContext ctx = new AdminUserContext())
            {
                BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx);
                var entity = await bs.GetAll().SingleAsync(e => e.Id == id);

                entity.PasswordHash = MD5Helper.CalcMD5(newPassword + entity.Salt);
                await ctx.SaveChangesAsync();
            }
        }
        public async Task UnLockAdminUserAsync(long id)
        {
            using (AdminUserContext ctx = new AdminUserContext())
            {
                BaseService <AdminUserEntity> userBs = new BaseService <AdminUserEntity>(ctx);
                var user = await userBs.GetAll().SingleAsync(e => e.Id == id);

                user.LoginErrorCount = 0;
                await ctx.SaveChangesAsync();
            }
        }
        public async Task ChangePasswordAsync(long id, string newPassword)
        {
            using (AdminUserContext ctx = new AdminUserContext())
            {
                BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx);
                AdminUserEntity entity           = await bs.GetAll().SingleOrDefaultAsync(e => e.Id == id);

                if (entity == null)
                {
                    throw new ArgumentException($"管理员{id}不存在");
                }
                entity.PasswordHash = MD5Helper.CalcMD5(newPassword + entity.Salt);
                await ctx.SaveChangesAsync();
            }
        }
Ejemplo n.º 8
0
        public async Task UpdateAsync(UpdateRolePermissionDTO dto)
        {
            using (AdminUserContext ctx = new AdminUserContext())
            {
                BaseService <RoleEntity> roleBs = new BaseService <RoleEntity>(ctx);
                var namePer = await roleBs.GetAll().SingleOrDefaultAsync(e => e.Name == dto.Name);

                if (namePer != null)
                {
                    if (dto.Id != namePer.Id)
                    {
                        throw new Exception("权限名已存在");
                    }
                }
                var entity = await roleBs.GetAll().SingleAsync(e => e.Id == dto.Id);

                entity.Description = dto.Description;
                entity.Name        = dto.Name;
                await ctx.SaveChangesAsync();
            }
        }
Ejemplo n.º 9
0
        public async Task <long> AddNewAsync(AddRolePermissionDTO dto)
        {
            RoleEntity entity = new RoleEntity();

            entity.Description = dto.Description;
            entity.Name        = dto.Name;
            using (AdminUserContext ctx = new AdminUserContext())
            {
                BaseService <RoleEntity> bs = new BaseService <RoleEntity>(ctx);
                var per = await bs.GetAll().SingleOrDefaultAsync(e => e.Name == dto.Name);

                if (per != null)
                {
                    throw new Exception("角色名已存在");
                }
                await ctx.Roles.AddAsync(entity);

                await ctx.SaveChangesAsync();

                return(entity.Id);
            }
        }