コード例 #1
0
ファイル: UserBiz.cs プロジェクト: khhily/Lean
        public UserInfo Add(UserInfo entity)
        {
            using (var db = new Entities())
            {
                if (CheckUser(entity, db))
                {
                    entity.CreateDate = DateTime.Now;
                    entity.CreateUserID = CurrentUserBiz.CurrentUser == null ? 0 : CurrentUserBiz.CurrentUser.ID;
                    entity.ModifyDate = DateTime.Now;
                    entity.ModifyUserID = CurrentUserBiz.CurrentUser == null ? 0 : CurrentUserBiz.CurrentUser.ID;

                    entity.Password = EncryPassword(entity.Password);

                    db.UserInfo.Add(entity);

                    Errors = db.GetErrors();

                    if (!HasError)
                    {
                        db.SaveChanges();
                    }
                }

                return entity;
            }
        }
コード例 #2
0
ファイル: ModuleBiz.cs プロジェクト: khhily/Lean
        public IEnumerable<BaseModule> GetModuleByUser(UserInfo user)
        {
            using (var db = new Entities())
            {
                var query = db.BaseModule.Where(q => q.Valid);

                query = (from q in query
                    join u in db.UserRight on q.ID equals u.ModuleID
                    where u.UserID == user.ID
                    select q);

                return query.OrderBy(q => q.ParentID).ThenBy(q => q.ModuleOrder).ToList();
            }
        }
コード例 #3
0
ファイル: UserManageController.cs プロジェクト: khhily/Lean
 public ActionResult AssignPermission(UserInfo entity)
 {
     UserBiz.AssignPermission(entity);
     SetMessage(UserBiz.HasError ? UserBiz.Errors.FirstOrDefault().Value : "授权成功!");
     return View(entity);
 }
コード例 #4
0
ファイル: UserBiz.cs プロジェクト: khhily/Lean
        public UserInfo AssignPermission(UserInfo entity)
        {
            using (var db = new Entities())
            {
                using (var scope = new TransactionScope())
                {
                    entity.ModifyDate = DateTime.Now;
                    entity.ModifyUserID = CurrentUserBiz.CurrentUser == null ? 0 : CurrentUserBiz.CurrentUser.ID;
                    //判断每个Module是否存在于RoleRight中
                    var role = db.UserRole.Include(q => q.UserRoleRight)
                        .FirstOrDefault(q => q.ID == entity.RoleID && q.Status == (int) StatusEnum.Valid);

                    if (role != null)
                    {
                        foreach (var newRight in entity.UserRight)
                        {
                            if (role.UserRoleRight.All(q => q.ModuleID != newRight.ModuleID))
                            {
                                var module = db.BaseModule.FirstOrDefault(q => q.ID == newRight.ModuleID && q.Valid);
                                Errors.Set("Error",
                                    module != null
                                        ? string.Format("角色({0})没有对模块({1})的权限!", role.RoleName, module.ModuleName)
                                        : string.Format("选择的权限超出了角色权限!"));
                            }
                        }

                        if (!HasError)
                        {
                            var oldUserRights = db.UserRight.Where(q => q.UserID == entity.ID).ToList();

                            foreach (var right in oldUserRights)
                            {
                                var userRight = db.UserRight.FirstOrDefault(q => q.ID == right.ID);
                                if (entity.UserRight.All(q => q.ModuleID != right.ModuleID))
                                {
                                    db.UserRight.Remove(userRight);
                                }
                            }
                            Errors = db.GetErrors();
                            if (!HasError)
                            {
                                try
                                {
                                    db.SaveChanges();

                                    var oldRights = db.UserRight.Where(q => q.UserID == entity.ID).ToList();
                                    foreach (var right in entity.UserRight)
                                    {
                                        if (oldRights.All(q => q.ModuleID != right.ModuleID))
                                        {
                                            db.UserRight.Add(new UserRight
                                            {
                                                UserID = entity.ID,
                                                ModuleID = right.ModuleID,
                                                CreateDate = entity.ModifyDate,
                                                CreateUserID = entity.ModifyUserID,
                                                ModifyUserID = entity.ModifyUserID,
                                                ModifyDate = entity.ModifyDate
                                            });
                                        }
                                    }

                                    Errors = db.GetErrors();
                                    if (!HasError)
                                    {
                                        db.SaveChanges();
                                        scope.Complete();
                                    }
                                }
                                catch (Exception e)
                                {
                                    Errors.Set("Error", e.InnerException == null ? e.Message : e.InnerException.Message);
                                }
                            }
                        }
                    }
                    else
                    {
                        Errors.Set("Error", "角色不存在!");
                    }
                }
            }
            return entity;
        }
コード例 #5
0
ファイル: UserBiz.cs プロジェクト: khhily/Lean
        public UserInfo Edit(UserInfo entity)
        {
            using (var db = new Entities())
            {
                using (var scope = new TransactionScope())
                {
                    if (CheckUser(entity, db))
                    {
                        entity.ModifyDate = DateTime.Now;
                        entity.ModifyUserID = CurrentUserBiz.CurrentUser == null ? 0 : CurrentUserBiz.CurrentUser.ID;
                        var user =
                            db.UserInfo.FirstOrDefault(q => q.Status == (int) StatusEnum.Valid && q.ID == entity.ID);
                        if (user != null)
                        {
                            //如果更改了角色, 就要修改权限
                            if (user.RoleID != entity.RoleID)
                            {
                                var newRole = db.UserRole.FirstOrDefault(q => q.ID == entity.RoleID);

                                foreach (var right in user.UserRight)
                                {
                                    var userRight = db.UserRight.FirstOrDefault(q => q.ID == right.ID);
                                    //如果现有权限没有在新的角色权限中,删除
                                    if (!newRole.UserRoleRight.Any(q => q.ModuleID == right.ModuleID))
                                    {
                                        db.UserRight.Remove(userRight);
                                    }
                                }

                                Errors = db.GetErrors();

                                if (!HasError)
                                {
                                    try
                                    {
                                        db.SaveChanges();
                                    }
                                    catch (Exception e)
                                    {
                                        Errors.Set("Error",
                                            e.InnerException == null ? e.Message : e.InnerException.Message);
                                    }
                                }
                                //
                            }
                            //处理密码
                            if (!string.IsNullOrWhiteSpace(entity.Password))
                            {
                                if (entity.Password.ToUpper().Trim() != user.Password.ToUpper().Trim())
                                {
                                    entity.Password = EncryPassword(entity.Password);
                                }
                            }


                            if (!HasError)
                            {
                                entity.CopyToOnly(user, q => q.IsAdmin, q => q.Email, q => q.ModifyDate,
                                    q => q.ModifyUserID, q => q.QQ, q => q.RoleID, 
                                    q => q.UserCode, q => q.UserGender, q => q.UserName,
                                    q => q.UserType);

                                Errors = db.GetErrors();
                                if (!HasError)
                                {
                                    db.SaveChanges();
                                    scope.Complete();
                                }
                            }
                        }
                        else
                        {
                            Errors.Set("Error", "用户不存在!");
                        }
                    }
                }
                return entity;
            }
        }
コード例 #6
0
ファイル: UserBiz.cs プロジェクト: khhily/Lean
 public bool CheckUser(UserInfo entity)
 {
     using (var db = new Entities())
     {
         return CheckUser(entity, db);
     }
 }
コード例 #7
0
ファイル: UserBiz.cs プロジェクト: khhily/Lean
        public bool CheckUser(UserInfo entity, Entities db)
        {
            var chkUsers = db.UserInfo.Where(q => q.ID != entity.ID && q.Status == (int) StatusEnum.Valid);
            if (chkUsers.Any(q => q.UserCode.ToUpper() == entity.UserCode.ToUpper()))
            {
                Errors.Set("repeat", "用户名已存在!");
                return false;
            }
            if (!db.UserRole.Any(q => q.ID == entity.RoleID))
            {
                Errors.Set("not exists", "用户角色不存在!");
                return false;
            }

            return true;
        }