Esempio n. 1
0
        /// <summary>
        /// 设置角色信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <SetRoleOutput> SetRole(SetRoleInput input)
        {
            var redis = _redisClient.GetDatabase(_configCenter.Get(SysConfig.RedisConnectionKey, "192.168.1.199:6379,allowadmin=true"), 2);
            var model = _mapper.Map <RoleInfo>(input);

            if (model.Id > 0)
            {
                model.UpdateTime = DateTime.Now;
                await _dbContext.Updateable(model).IgnoreColumns(it => new { it.CreateTime, it.IsSys }).ExecuteCommandAsync();
            }
            else
            {
                model.CreateTime = DateTime.Now;
                model.IsDel      = false;
                await _dbContext.Insertable(model).ExecuteCommandAsync();
            }
            await redis.KeyDeleteAsync(CacheKeys.RoleAllUseKey);

            return(new SetRoleOutput {
            });
        }
Esempio n. 2
0
        /// <summary>
        /// 设置角色信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <SetRoleOutput> SetRole(SetRoleInput input)
        {
            try
            {
                _dbContext.Ado.BeginTran();

                #region 基础信息更新
                var redis = _redisClient.GetDatabase(_configCenter.StringGet(SysConfig.RedisConnectionKey), 2);
                var model = _mapper.Map <RoleInfo>(input);
                if (model.Id > 0)
                {
                    model.UpdateTime = DateTime.Now;
                    await _dbContext.Updateable(model)
                    .IgnoreColumns(it => new { it.PlatformKey, it.CreateTime, it.IsSys })
                    .ExecuteCommandAsync();
                }
                else
                {
                    model.CreateTime = DateTime.Now;
                    model.IsDel      = false;
                    model.Id         = await _dbContext.Insertable(model)
                                       .ExecuteReturnIdentityAsync();
                }
                #endregion

                #region 菜单权限
                // 用户角色操作
                List <RoleMenuInfo> roleMenuList = new List <RoleMenuInfo>();
                foreach (var id in input.MenuIdList)
                {
                    // 防止重复数据
                    if (!roleMenuList.Exists(it => it.MenuId == id))
                    {
                        roleMenuList.Add(new RoleMenuInfo
                        {
                            MenuId = id,
                            RoleId = model.Id
                        });
                    }
                }
                // 删除用户当前角色
                await _dbContext.Deleteable <RoleMenuInfo>().Where(f => f.RoleId == model.Id).ExecuteCommandAsync();

                // 添加用户角色
                if (roleMenuList.Count > 0)
                {
                    await _dbContext.Insertable(roleMenuList).ExecuteCommandAsync();
                }
                #endregion

                #region 菜单权限
                // 用户角色操作
                List <RoleApiInfo> roleApiList = new List <RoleApiInfo>();
                foreach (var id in input.ApiIdList)
                {
                    // 防止重复数据
                    if (!roleApiList.Exists(it => it.ApiId == id))
                    {
                        roleApiList.Add(new RoleApiInfo
                        {
                            ApiId  = id,
                            RoleId = model.Id
                        });
                    }
                }
                // 删除用户当前角色
                await _dbContext.Deleteable <RoleApiInfo>().Where(f => f.RoleId == model.Id).ExecuteCommandAsync();

                // 添加用户角色
                if (roleApiList.Count > 0)
                {
                    await _dbContext.Insertable(roleApiList).ExecuteCommandAsync();
                }
                #endregion

                #region 缓存更新
                await redis.KeyDeleteAsync(CacheKeys.RoleAllUseKey);

                // 应该立即更新缓存
                #endregion

                _dbContext.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                _dbContext.Ado.RollbackTran();
                throw new Exception("事务执行失败", ex);
            }
            return(new SetRoleOutput {
            });
        }
Esempio n. 3
0
        public BaseOutput SetRole([FromBody] SetRoleInput input)
        {
            try
            {
                _adminDbContext.Ado.BeginTran();

                #region 基础信息更新
                var model = _mapper.Map <RoleModel>(input);
                if (model.Id > 0)
                {
                    model.UpdateTime = DateTime.Now;
                    _adminDbContext.Updateable(model)
                    .IgnoreColumns(it => new { it.PlatformKey, it.CreateTime, it.IsSys })
                    .ExecuteCommand();
                }
                else
                {
                    model.CreateTime = DateTime.Now;
                    model.IsDel      = false;
                    model.Id         = _adminDbContext.Insertable(model).ExecuteReturnIdentity();
                }
                #endregion

                #region 菜单权限
                // 用户角色操作
                List <RoleMenuModel> roleMenuList = new List <RoleMenuModel>();
                foreach (var id in input.MenuIdList)
                {
                    // 防止重复数据
                    if (!roleMenuList.Exists(it => it.MenuId == id))
                    {
                        roleMenuList.Add(new RoleMenuModel
                        {
                            MenuId = id,
                            RoleId = model.Id
                        });
                    }
                }
                // 删除用户当前角色
                _adminDbContext.Deleteable <RoleMenuModel>().Where(f => f.RoleId == model.Id).ExecuteCommand();
                // 添加用户角色
                if (roleMenuList.Count > 0)
                {
                    _adminDbContext.Insertable(roleMenuList).ExecuteCommand();
                }
                #endregion

                #region 接口权限
                // 用户角色操作
                List <RoleApiModel> roleApiList = new List <RoleApiModel>();
                foreach (var id in input.ApiIdList)
                {
                    // 防止重复数据
                    if (!roleApiList.Exists(it => it.ApiId == id))
                    {
                        roleApiList.Add(new RoleApiModel
                        {
                            ApiId  = id,
                            RoleId = model.Id
                        });
                    }
                }
                // 删除用户当前角色
                _adminDbContext.Deleteable <RoleApiModel>().Where(f => f.RoleId == model.Id).ExecuteCommand();
                // 添加用户角色
                if (roleApiList.Count > 0)
                {
                    _adminDbContext.Insertable(roleApiList).ExecuteCommand();
                }
                #endregion

                _adminDbContext.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                _adminDbContext.Ado.RollbackTran();
                throw new Exception("事务执行失败", ex);
            }
            return(new BaseOutput {
            });
        }
Esempio n. 4
0
 public async Task <SetRoleOutput> SetRole([FromBody] SetRoleInput input)
 {
     return(await _roleBusiness.SetRole(input));
 }
Esempio n. 5
0
        /// <summary>
        /// 设置角色信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <SetRoleOutput> SetRole(SetRoleInput input)
        {
            #region 基础信息更新
            var redis = _redisClient.GetDatabase(_configCenter.Get(SysConfig.RedisConnectionKey, "192.168.1.199:6379,allowadmin=true"), 2);
            var model = _mapper.Map <RoleInfo>(input);
            if (model.Id > 0)
            {
                model.UpdateTime = DateTime.Now;
                await _dbContext.Updateable(model)
                .IgnoreColumns(it => new { it.ProjectName, it.CreateTime, it.IsSys })
                .ExecuteCommandAsync();
            }
            else
            {
                model.CreateTime = DateTime.Now;
                model.IsDel      = false;
                model.Id         = await _dbContext.Insertable(model)
                                   .ExecuteReturnIdentityAsync();
            }
            #endregion

            #region 菜单权限
            // 用户角色操作
            List <RoleMenuInfo> roleMenuList = new List <RoleMenuInfo>();
            foreach (var id in input.MenuIdList)
            {
                // 防止重复数据
                if (!roleMenuList.Exists(it => it.MenuId == id))
                {
                    roleMenuList.Add(new RoleMenuInfo
                    {
                        MenuId = id,
                        RoleId = model.Id
                    });
                }
            }
            // 删除用户当前角色
            await _dbContext.Deleteable <RoleMenuInfo>().Where(f => f.RoleId == model.Id).ExecuteCommandAsync();

            // 添加用户角色
            if (roleMenuList.Count > 0)
            {
                await _dbContext.Insertable(roleMenuList).ExecuteCommandAsync();
            }
            #endregion

            #region 菜单权限
            // 用户角色操作
            List <RoleApiInfo> roleApiList = new List <RoleApiInfo>();
            foreach (var id in input.ApiIdList)
            {
                // 防止重复数据
                if (!roleApiList.Exists(it => it.ApiId == id))
                {
                    roleApiList.Add(new RoleApiInfo
                    {
                        ApiId  = id,
                        RoleId = model.Id
                    });
                }
            }
            // 删除用户当前角色
            await _dbContext.Deleteable <RoleApiInfo>().Where(f => f.RoleId == model.Id).ExecuteCommandAsync();

            // 添加用户角色
            if (roleApiList.Count > 0)
            {
                await _dbContext.Insertable(roleApiList).ExecuteCommandAsync();
            }
            #endregion

            #region 缓存更新
            await redis.KeyDeleteAsync(CacheKeys.RoleAllUseKey);

            // 应该立即更新缓存
            #endregion

            return(new SetRoleOutput {
            });
        }