예제 #1
0
        /// <summary>
        /// 插入用户角色对应关系记录
        /// </summary>
        /// <param name="sys_UserRole">用户角色对应关系对象</param>
        /// <returns></returns>
        public int InsertSys_UserRole(Sys_UserRole sys_UserRole)
        {
            string sql = @"INSERT INTO dbo.Sys_UserRole(ProjID, PersonID, RoleId, Status, CreatedBy) VALUES(@ProjID, @PersonID, @RoleId, @Status, @CreatedBy); SELECT @KeyId = SCOPE_IDENTITY()";

            Database  db      = DatabaseFactory.CreateDatabase(DBLink.SysDBLink.ToString());
            DbCommand command = db.GetSqlStringCommand(sql);

            db.AddOutParameter(command, "KeyId", DbType.Int32, sizeof(int));
            db.AddInParameter(command, "ProjID", DbType.Int32, sys_UserRole.ProjID.HasValue ? (object)sys_UserRole.ProjID : DBNull.Value);
            db.AddInParameter(command, "PersonID", DbType.Int32, sys_UserRole.PersonID);
            db.AddInParameter(command, "RoleId", DbType.Int32, sys_UserRole.RoleId);
            db.AddInParameter(command, "Status", DbType.Boolean, sys_UserRole.Status);
            db.AddInParameter(command, "CreatedBy", DbType.Int32, sys_UserRole.CreatedBy);

            int affectedRecords = db.ExecuteNonQuery(command);

            if (affectedRecords < 1)
            {
                throw new ApplicationException("插入数据失败, 没有记录被插入");
            }
            else
            {
                string    strTemp = "select @@identity";
                DataTable dt      = GetDataTable(strTemp);
                if (dt != null && dt.Rows.Count > 0)
                {
                    affectedRecords = int.Parse(dt.Rows[0][0].ToString());
                }
            }
            return(affectedRecords);
        }
예제 #2
0
        /// <summary>
        ///根据主键值查找用户角色对应关系记录
        /// </summary>
        /// <param name="keyId">主键</param>
        /// <returns>Sys_UserRole</returns>
        public Sys_UserRole FindSys_UserRole(int keyId)
        {
            string sql = @"SELECT KeyId, ProjID, PersonID, RoleId, Status, CreatedBy, CreatedOn, ModifiedBy, ModifiedOn FROM dbo.Sys_UserRole WHERE KeyId = @KeyId";

            Database  db      = DatabaseFactory.CreateDatabase(DBLink.SysDBLink.ToString());
            DbCommand command = db.GetSqlStringCommand(sql);

            db.AddInParameter(command, "KeyId", DbType.Int32, keyId);

            Sys_UserRole sys_UserRole = null;

            using (IDataReader dr = db.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    sys_UserRole = new Sys_UserRole();

                    sys_UserRole.KeyId      = (int)dr["KeyId"];
                    sys_UserRole.ProjID     = dr["ProjID"] == DBNull.Value ? null : (int?)dr["ProjID"];
                    sys_UserRole.PersonID   = (int)dr["PersonID"];
                    sys_UserRole.RoleId     = (int)dr["RoleId"];
                    sys_UserRole.Status     = (bool)dr["Status"];
                    sys_UserRole.CreatedBy  = (int)dr["CreatedBy"];
                    sys_UserRole.CreatedOn  = (DateTime)dr["CreatedOn"];
                    sys_UserRole.ModifiedBy = dr["ModifiedBy"] == DBNull.Value ? null : (int?)dr["ModifiedBy"];
                    sys_UserRole.ModifiedOn = dr["ModifiedOn"] == DBNull.Value ? null : (DateTime?)dr["ModifiedOn"];
                }
            }

            return(sys_UserRole);
        }
예제 #3
0
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="model"></param>
        /// <param name="_Sys_Function_List"></param>
        /// <returns></returns>
        public string Save(Sys_User model, Sys_UserRole _Sys_UserRole)
        {
            db.Commit(() =>
            {
                if (model.User_ID.ToGuid() == Guid.Empty)
                {
                    if (string.IsNullOrEmpty(model.User_Pwd))
                    {
                        model.User_Pwd = "123"; //Tools.MD5Encrypt("123456");
                    }
                    else
                    {
                        model.User_Pwd = model.User_Pwd;//Tools.MD5Encrypt(model.cUsers_LoginPwd);
                    }
                    model.User_ID = db.Insert(model).ToGuid();
                    if (model.User_ID.ToGuid().Equals(Guid.Empty))
                    {
                        throw new MessageBox(this.ErrorMessge);
                    }
                    //用户角色
                    _Sys_UserRole.UserRole_UserID = model.User_ID;
                    _Sys_UserRole.UserRole_ID     = db.Insert(_Sys_UserRole).ToGuid();
                    if (_Sys_UserRole.UserRole_ID == Guid.Empty)
                    {
                        throw new MessageBox(this.ErrorMessge);
                    }
                }
                else
                {
                    //如果 密码字段为空,则不修改该密码
                    if (string.IsNullOrEmpty(model.User_Pwd))
                    {
                        db.Update <Sys_User>(w => w.User_ID == model.User_ID, () => new Sys_User
                        {
                            User_ID        = model.User_ID,
                            User_Email     = model.User_Email,
                            User_IsDelete  = model.User_IsDelete,
                            User_LoginName = model.User_LoginName,
                            User_Name      = model.User_Name
                        });
                    }
                    else
                    {
                        if (!db.UpdateById(model))
                        {
                            throw new MessageBox(this.ErrorMessge);
                        }
                    }

                    //用户角色
                    db.Update <Sys_UserRole>(w => w.UserRole_UserID == model.User_ID, () => new Sys_UserRole
                    {
                        UserRole_RoleID = _Sys_UserRole.UserRole_RoleID
                    });
                }
            });

            return(model.User_ID.ToGuidStr());
        }
예제 #4
0
        /// <summary>
        /// 返回满足查询条件的用户角色对应关系实体列表
        /// </summary>
        /// <param name="param">查询条件</param>
        /// <returns>用户角色对应关系实体列表</returns>
        public IList <Sys_UserRole> GetSys_UserRoles(QueryParameter param)
        {
            string sql = @"SELECT KeyId, ProjID, PersonID, RoleId, Status, CreatedBy, CreatedOn, ModifiedBy, ModifiedOn FROM dbo.Sys_UserRole";

            if (param != null)
            {
                sql = QueryParameter.CompleteSqlString(sql, param);
            }

            Database  db      = DatabaseFactory.CreateDatabase(DBLink.SysDBLink.ToString());
            DbCommand command = db.GetSqlStringCommand(sql);

            if (param != null)
            {
                //设置参数
                foreach (IExpression exp in param.WhereExpressions)
                {
                    if (exp is SimpleExpression)
                    {
                        SimpleExpression simple = exp as SimpleExpression;
                        db.AddInParameter(command, simple.ExpName, simple.DbType, simple.Value);
                    }
                }
            }

            IList <Sys_UserRole> list = new List <Sys_UserRole>();

            using (IDataReader dr = db.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    Sys_UserRole sys_UserRole = new Sys_UserRole();

                    sys_UserRole.KeyId      = (int)dr["KeyId"];
                    sys_UserRole.ProjID     = dr["ProjID"] == DBNull.Value ? null : (int?)dr["ProjID"];
                    sys_UserRole.PersonID   = (int)dr["PersonID"];
                    sys_UserRole.RoleId     = (int)dr["RoleId"];
                    sys_UserRole.Status     = (bool)dr["Status"];
                    sys_UserRole.CreatedBy  = (int)dr["CreatedBy"];
                    sys_UserRole.CreatedOn  = (DateTime)dr["CreatedOn"];
                    sys_UserRole.ModifiedBy = dr["ModifiedBy"] == DBNull.Value ? null : (int?)dr["ModifiedBy"];
                    sys_UserRole.ModifiedOn = dr["ModifiedOn"] == DBNull.Value ? null : (DateTime?)dr["ModifiedOn"];

                    list.Add(sys_UserRole);
                }
            }

            return(list);
        }
예제 #5
0
        /// <summary>
        /// 更新用户角色对应关系记录
        /// </summary>
        /// <param name="sys_UserRole">用户角色对应关系对象</param>
        /// <returns>受影响的记录数</returns>
        public int UpdateSys_UserRole(Sys_UserRole sys_UserRole)
        {
            string sql = @"UPDATE dbo.Sys_UserRole SET ProjID = @ProjID, PersonID = @PersonID, RoleId = @RoleId, Status = @Status, ModifiedBy = @ModifiedBy, ModifiedOn = @ModifiedOn WHERE KeyId = @KeyId";

            Database  db      = DatabaseFactory.CreateDatabase(DBLink.SysDBLink.ToString());
            DbCommand command = db.GetSqlStringCommand(sql);

            db.AddInParameter(command, "KeyId", DbType.Int32, sys_UserRole.KeyId);
            db.AddInParameter(command, "ProjID", DbType.Int32, sys_UserRole.ProjID.HasValue ? (object)sys_UserRole.ProjID : DBNull.Value);
            db.AddInParameter(command, "PersonID", DbType.Int32, sys_UserRole.PersonID);
            db.AddInParameter(command, "RoleId", DbType.Int32, sys_UserRole.RoleId);
            db.AddInParameter(command, "Status", DbType.Boolean, sys_UserRole.Status);
            db.AddInParameter(command, "ModifiedBy", DbType.Int32, sys_UserRole.ModifiedBy.HasValue ? (object)sys_UserRole.ModifiedBy : DBNull.Value);
            db.AddInParameter(command, "ModifiedOn", DbType.DateTime, sys_UserRole.ModifiedOn.HasValue ? (object)sys_UserRole.ModifiedOn : DBNull.Value);

            return(db.ExecuteNonQuery(command));
        }
        /// <summary>
        /// 新增\修改
        /// </summary>
        /// <param name="Dto"></param>
        /// <returns></returns>
        public async Task <Guid> SaveAsync(Sys_UserDto Dto)
        {
            var model   = Dto.Model;
            var roleIds = Dto.RoleIds;

            if (string.IsNullOrWhiteSpace(model.User_Pwd))
            {
                MessageBox.Show("密码不能为空!");
            }

            if (model.User_ID == Guid.Empty)
            {
                model.User_Pwd = string.IsNullOrWhiteSpace(model.User_Pwd) ? "123" : model.User_Pwd; //Tools.MD5Encrypt("123");
                model          = await this.InsertAsync(model);
            }
            else
            {
                await this.UpdateByIdAsync(model);
            }

            //变更用户角色
            if (roleIds.Count > 0)
            {
                var _Sys_UserRoleList = await dbUserRole.ToListAsync(w => w.UserRole_UserID == model.User_ID);

                await dbUserRole.DeleteAsync(w => w.UserRole_UserID == model.User_ID);

                foreach (var item in roleIds)
                {
                    var _Sys_UserRole = _Sys_UserRoleList.FirstOrDefault(w => w.UserRole_RoleID == item);

                    var userRoleModel = new Sys_UserRole();
                    userRoleModel.UserRole_ID = _Sys_UserRole == null?Guid.NewGuid() : _Sys_UserRole.UserRole_ID;

                    userRoleModel.UserRole_RoleID = item;
                    userRoleModel.UserRole_UserID = model.User_ID;
                    await dbUserRole.InsertAsync(userRoleModel);
                }
            }

            return(model.User_ID);
        }
예제 #7
0
        /// <summary>
        /// 更新Sys_UserRole
        /// </summary>
        /// <param name="sys_UserRole">实体类</param>
        /// <param name="logEntity">日志类</param>
        /// <returns></returns>
        public string UpdateSys_UserRole(Sys_UserRole sys_UserRole, Log_Operate logEntity)
        {
            string strResult = "";

            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    this._sys_UserRoleDAO.UpdateSys_UserRole(sys_UserRole);
                    Log_OperateFacade logFacade = new Log_OperateFacade();
                    int intLog = logFacade.CreateLog_Operate(logEntity);
                    trans.Complete();
                }
                catch (Exception ex)
                {
                    strResult = ex.Message;
                }
            }
            return(strResult);
        }
예제 #8
0
        /// <summary>
        /// 创建Sys_UserRole
        /// </summary>
        /// <param name="sys_UserRole">实体类</param>
        /// <param name="logEntity">日志类</param>
        /// <param name="strResult">错误信息</param>
        /// <returns></returns>
        public int InsertSys_UserRole(Sys_UserRole sys_UserRole, Log_Operate logEntity, ref string strResult)
        {
            int intResult = 0;

            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    this._sys_UserRoleDAO.InsertSys_UserRole(sys_UserRole);
                    Log_OperateFacade logFacade = new Log_OperateFacade();
                    logEntity.OperateFunction = "新增_sys_UserRole表ID为" + intResult.ToString() + "的数据";
                    int intLog = logFacade.CreateLog_Operate(logEntity);
                    trans.Complete();
                }
                catch (Exception ex)
                {
                    strResult = ex.Message;
                }
            }
            return(intResult);
        }
예제 #9
0
        public async Task <bool> AddUsersToRole(List <long> uIds, int roleId)
        {
            //1:检查用户是否都正常
            var userIds = _accountRepository.GetAll().Where(u => uIds.Contains(u.Id)).Select(u => u.Id).ToList();

            //2:排除掉已经存在该角色的人员
            var userRoleIds = _userRoleRepository.GetAll().Where(u => u.RoleId.Equals(roleId)).Select(u => u.UserId).ToList();

            //3:移除匹配项
            userIds.RemoveAll(u => userRoleIds.Contains(u));
            //2:管理用户和角色权限
            userIds.ForEach(async u =>
            {
                Sys_UserRole entity = new Sys_UserRole()
                {
                    UserId = u,
                    RoleId = roleId
                };
                await _userRoleRepository.InsertAsync(entity);
            });
            return(await Task.FromResult(true));
        }
예제 #10
0
 public async Task <object> Update(Sys_UserRole ent)
 {
     return(_logic.GetDbClient().GetSimpleClient <Sys_UserRole>().Update(ent));
 }
예제 #11
0
 public async Task <object> InsertReturnIdentity(Sys_UserRole ent)
 {
     return(_logic.GetDbClient().GetSimpleClient <Sys_UserRole>().InsertReturnIdentity(ent));
 }
예제 #12
0
 /// <summary>
 /// 更新用户角色对应关系记录
 /// </summary>
 /// <param name="sys_UserRole">
 /// 用户角色对应关系对象</param>
 /// <returns>受影响的记录数</returns>
 public int UpdateSys_UserRole(Sys_UserRole sys_UserRole)
 {
     return(this._sys_UserRoleDAO.UpdateSys_UserRole(sys_UserRole));
 }
예제 #13
0
 /// <summary>
 /// 创建用户角色对应关系记录
 /// </summary>
 /// <param name="sys_UserRole">
 /// 用户角色对应关系对象</param>
 /// <returns></returns>
 public int CreateSys_UserRole(Sys_UserRole sys_UserRole)
 {
     return(this._sys_UserRoleDAO.InsertSys_UserRole(sys_UserRole));
 }
예제 #14
0
        public IActionResult UpdateUserRole([FromBody] FromUpdateSysUser model)
        {
            string code       = "000000";
            var    userEntity = _sysUserService.GetUserById(model.UserId);

            if (userEntity != null)
            {
                try
                {
                    var now = DateTime.Now;
                    //异步更新用户角色相关
                    Task.Run(() =>
                    {
                        var user        = AutoMapperExt.MapTo <Sys_User>(model);
                        user.Id         = model.UserId;
                        user.ModifyDate = now;
                        if (string.IsNullOrEmpty(model.Password))
                        {
                            _sysUserService.UpdateColumns(p => new { p.UserName, p.RealName, p.Email, p.HeadImgUrl, p.Mobile, p.Sex, p.ModifyDate }, user, true);
                        }
                        else
                        {
                            user.Password = Encrypt.EncryptPsw(model.Password);
                            _sysUserService.UpdateColumns(p => new { p.UserName, p.RealName, p.Password, p.Email, p.HeadImgUrl, p.Mobile, p.Sex, p.ModifyDate }, user, true);
                        }
                    });
                    //异步更新图片状态
                    if (userEntity.HeadImgUrl != model.HeadImgUrl)
                    {
                        _sysUploadService.UpdateUploadStatusAsync(UploadTypeEnum.image, model.HeadImgUrl, UploadStatusEnum.使用中);
                        if (!string.IsNullOrEmpty(userEntity.HeadImgUrl))
                        {
                            _sysUploadService.UpdateUploadStatusAsync(UploadTypeEnum.image, userEntity.HeadImgUrl, UploadStatusEnum.可删除);
                        }
                    }
                    //异步添加用户角色权限
                    if (model.RoleList.Where(p => p.Status == (int)RoleMenuStatus.add).Count() > 0 && model.UserId > 0)
                    {
                        Task.Run(() =>
                        {
                            var perList  = new List <Sys_UserRole>();
                            var menuList = model.RoleList.Where(p => p.Status == (int)RoleMenuStatus.add);

                            foreach (var item in menuList)
                            {
                                var perModel    = new Sys_UserRole();
                                perModel.RoleId = item.RoleId;
                                perModel.UserId = model.UserId;
                                perList.Add(perModel);
                            }
                            _sysUserService.InsertList(perList);
                        });
                    }
                    //异步删除用户角色权限
                    if (model.RoleList.Where(p => p.Status == (int)RoleMenuStatus.delete).Count() > 0 && model.UserId > 0)
                    {
                        Task.Run(() =>
                        {
                            var perList = model.RoleList.Where(p => p.Status == (int)RoleMenuStatus.delete).Select(p => p.UserRoleId).ToArray();
                            _sysUserService.DeleteByIdArray(perList);
                        });
                    }
                }
                catch (Exception ex)
                {
                    _log.LogError(ex.ToString());
                    code = "000100";
                }
            }
            else
            {
                code = "100007";
            }
            return(ReturnJson(code));
        }
예제 #15
0
        public IActionResult AddSysUser([FromBody] FromAddSysUser model)
        {
            string code = "000000";
            var    user = AutoMapperExt.MapTo <Sys_User>(model);

            user.Password = Encrypt.EncryptPsw(user.Password);

            Log_Admin logAdmin = new Log_Admin();

            var now = DateTime.Now;

            if (_sysUserService.IsAny(p => p.UserName == user.UserName))
            {
                return(ReturnJson("100001"));
            }
            else
            {
                user.LastIp      = HttpContextExtension.GetIp(HttpContext);
                user.LastLogDate = now;
                user.CrtUser     = GetJwtIEntity().Name;
                user.CrtDate     = now;
                user.Id          = _sysUserService.Insert(user, false);

                if (user.Id > 0 && !string.IsNullOrEmpty(user.HeadImgUrl))
                {
                    //更改图片状态
                    _sysUploadService.UpdateUploadStatusAsync(UploadTypeEnum.image, user.HeadImgUrl, UploadStatusEnum.使用中);
                }

                if (user.Id > 0 && model.RoleList.Count > 0)
                {
                    //异步执行角色权限配置
                    Task.Run(() =>
                    {
                        var perList = new List <Sys_UserRole>();
                        foreach (var item in model.RoleList)
                        {
                            var perModel    = new Sys_UserRole();
                            perModel.RoleId = item.RoleId;
                            perModel.UserId = user.Id;
                            perList.Add(perModel);
                        }
                        _sysUserService.InsertList(perList);
                    });
                }
                else if (user.Id == 0)
                {
                    code = "000001";
                }

                logAdmin.Remark = "账号:" + user.UserName + "注册成功";

                logAdmin.TypeId = (int)EnumLogAdminType.add_sysUser;//添加用户时的类型Id
            }

            logAdmin.OtherId     = user.Id.ToString();
            logAdmin.CrtUserId   = GetJwtIEntity().UserId;
            logAdmin.CrtUserName = GetJwtIEntity().Name;
            _logAdmin.LogAdmin(logAdmin, HttpContext);
            return(ReturnJson(code));
        }