Example #1
0
 /// <summary>
 /// 查询所有权限字典
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <param name="role">角色</param>
 /// <param name="rights">权限列表</param>
 public static void SelectAllRights(WarehouseEntryDbContext dbContext, SecurityRole role, List<SystemRight> rights)
 {
     if (dbContext == null)
     {
         throw new ArgumentNullException("dbContext");
     }
     if (role == null)
     {
         throw new ArgumentNullException("role");
     }
     if (string.IsNullOrWhiteSpace(role.RoleName))
     {
         throw new InvalidEnumArgumentException("role");
     }
     rights.ForEach(r => r.Enabled = dbContext.SystemRight.Any(dr =>
         dr.SecurityRole.RoleName == role.RoleName
         && dr.SystemMenu.MenuName == r.SystemMenu.MenuName
         && dr.Enabled));
 }
Example #2
0
 /// <summary>
 /// 添加菜单列表
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <param name="parentMenu">父级菜单名</param>
 /// <param name="menus">菜单名列表</param>
 public static void AppendSystemMenus(WarehouseEntryDbContext dbContext, string parentMenu, List<string> menus)
 {
     if (dbContext == null)
     {
         throw new ArgumentNullException("dbContext");
     }
     if (menus == null)
     {
         throw new ArgumentNullException("menus");
     }
     if (menus.Any(string.IsNullOrWhiteSpace))
     {
         throw new InvalidEnumArgumentException("menus");
     }
     SystemMenu parentDataMenu = null;
     if (!string.IsNullOrWhiteSpace(parentMenu))
     {
         parentDataMenu = dbContext.SystemMenu.FirstOrDefault(m => m.MenuName == parentMenu);
     }
     if (parentDataMenu == null)
     {
         parentDataMenu = new SystemMenu { MenuName = parentMenu, Enabled = true };
     }
     List<SystemMenu> dataMenus = dbContext.SystemMenu.Where(m => menus.Contains(m.MenuName)).ToList();
     foreach (SystemMenu dataMenu in dataMenus)
     {
         menus.Remove(dataMenu.MenuName);
     }
     dbContext.SystemMenu.AddRange(menus.Select(m => new SystemMenu
     {
         ParentMenu = parentDataMenu,
         MenuName = m,
         Enabled = true
     }));
     dbContext.SaveChanges();
 }
Example #3
0
 /// <summary>
 /// 查询用户权限字典
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <param name="role">角色实体</param>
 /// <param name="menus">菜单名称列表</param>
 /// <returns></returns>
 public static List<SystemRight> SelectRights(WarehouseEntryDbContext dbContext, SecurityRole role, string[] menus)
 {
     if (dbContext == null)
     {
         throw new ArgumentNullException("dbContext");
     }
     if (role == null)
     {
         throw new ArgumentNullException("role");
     }
     if (string.IsNullOrWhiteSpace(role.RoleName))
     {
         throw new InvalidEnumArgumentException("role");
     }
     if (menus == null)
     {
         throw new ArgumentNullException("menus");
     }
     List<SystemMenu> dataMenus = dbContext.SystemMenu.Where(m => menus.Contains(m.MenuName)).ToList();
     return dataMenus.Select(dataMenu => dbContext.SystemRight.FirstOrDefault(r => r.SecurityRole.RoleName == role.RoleName
         && r.SystemMenu.MenuName == dataMenu.MenuName)
         ?? new SystemRight
         {
             SecurityRole = role, SystemMenu = dataMenu
         }).ToList();
 }
Example #4
0
 ///// <summary>
 ///// 查询用户列表
 ///// </summary>
 ///// <param name="dbContext">DbContext</param>
 ///// <param name="roleName">角色名</param>
 ///// <param name="userNameKey">用户名关键字</param>
 ///// <param name="pageIndex">分页页码</param>
 ///// <returns></returns>
 //public static List<SecurityUser> SelectUsers(WarehouseEntryDbContext dbContext, string roleName, string userNameKey, int pageIndex)
 //{
 //    if (dbContext == null)
 //    {
 //        throw new ArgumentNullException("dbContext");
 //    }
 //    IQueryable<SecurityUser> query = dbContext.SecurityUser;
 //    if (!string.IsNullOrWhiteSpace(roleName))
 //    {
 //        query = query.Where(u => u.SecurityRole.RoleName == roleName);
 //    }
 //    if (!string.IsNullOrWhiteSpace(userNameKey))
 //    {
 //        query = query.Where(u => u.UserName.Contains(userNameKey));
 //    }
 //    PagingData paging = new PagingData { PageIndex = pageIndex };
 //    return paging.SelectFootPagingData(query, u => u.CreationTime).ToList();
 //}
 /// <summary>
 /// 查询用户列表
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <param name="roleName">角色名</param>
 /// <param name="userNameKey">用户名关键字</param>
 /// <returns></returns>
 public static List<SecurityUser> SelectUsers(WarehouseEntryDbContext dbContext, string roleName, string userNameKey)
 {
     if (dbContext == null)
     {
         throw new ArgumentNullException("dbContext");
     }
     if (string.IsNullOrWhiteSpace(roleName))
     {
         throw new ArgumentNullException("roleName");
     }
     IQueryable<SecurityUser> query = dbContext.SecurityUser.Where(u => u.SecurityRole.RoleName == roleName);
     if (!string.IsNullOrWhiteSpace(userNameKey))
     {
         query = query.Where(u => u.UserName.Contains(userNameKey));
     }
     return query.OrderBy(u => u.UserName).ToList();
 }
Example #5
0
 /// <summary>
 /// 查询用户实体
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <param name="userName">用户名</param>
 /// <returns></returns>
 public static SecurityUser SelectUser(WarehouseEntryDbContext dbContext, string userName)
 {
     if (dbContext == null)
     {
         throw new ArgumentNullException("dbContext");
     }
     if (string.IsNullOrWhiteSpace(userName))
     {
         throw new ArgumentNullException("userName");
     }
     return dbContext.SecurityUser.FirstOrDefault(u => u.UserName == userName);
 }
Example #6
0
 /// <summary>
 /// 查询角色列表
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <returns></returns>
 public static List<SecurityRole> SelectRoles(WarehouseEntryDbContext dbContext)
 {
     if (dbContext == null)
     {
         throw new ArgumentNullException("dbContext");
     }
     return dbContext.SecurityRole.ToList();
 }
Example #7
0
 /// <summary>
 /// 查询菜单列表
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <param name="menus">菜单名列表</param>
 /// <returns></returns>
 public static List<SystemMenu> SelectSystemMenus(WarehouseEntryDbContext dbContext, string[] menus)
 {
     if (dbContext == null)
     {
         throw new ArgumentNullException("dbContext");
     }
     if (menus == null)
     {
         throw new ArgumentNullException("menus");
     }
     if (menus.Any(string.IsNullOrWhiteSpace))
     {
         throw new InvalidEnumArgumentException("menus");
     }
     return dbContext.SystemMenu.Where(m => menus.Contains(m.MenuName)).ToList();
 }
Example #8
0
 /// <summary>
 /// 查询角色实体
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <param name="roleName">角色名</param>
 /// <returns></returns>
 public static SecurityRole SelectRole(WarehouseEntryDbContext dbContext, string roleName)
 {
     if (dbContext == null)
     {
         throw new ArgumentNullException("dbContext");
     }
     if (string.IsNullOrWhiteSpace(roleName))
     {
         throw new ArgumentNullException("roleName");
     }
     return dbContext.SecurityRole.FirstOrDefault(r => r.RoleName == roleName);
 }
Example #9
0
 /// <summary>
 /// 查询有权限的用户列表
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <param name="menuName">菜单名</param>
 /// <returns></returns>
 public static List<SecurityUser> SelectRightUsers(WarehouseEntryDbContext dbContext, string menuName)
 {
     if (dbContext == null)
     {
         throw new ArgumentNullException("dbContext");
     }
     if (string.IsNullOrWhiteSpace(menuName))
     {
         throw new ArgumentNullException("menuName");
     }
     return dbContext.SecurityUser.Where(u => u.Enabled && u.SecurityRole.Enabled
         && dbContext.SystemRight.Any(r => r.SystemMenu.MenuName == menuName
             && r.SecurityRole.RoleName == u.SecurityRole.RoleName && r.Enabled)).ToList();
 }
Example #10
0
 /// <summary>
 /// 获取数据库对象,并进行密码校验
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <param name="userName">用户名</param>
 /// <param name="password">密码</param>
 /// <returns></returns>
 private static SecurityUser GetSecurityUser(WarehouseEntryDbContext dbContext, string userName, string password)
 {
     SecurityUser user = GetSecurityUser(dbContext, userName);
     if (!user.Enabled)
     {
         throw new ErrorException("UserDisabled", userName);
     }
     password = EncryptPassword(password);
     if (user.Password != password)
     {
         throw new ErrorException("PasswordError");
     }
     return user;
 }
Example #11
0
 /// <summary>
 /// 获取数据库对象
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <param name="userName">用户名</param>
 /// <returns></returns>
 public static SecurityUser GetSecurityUser(WarehouseEntryDbContext dbContext, string userName)
 {
     SecurityUser user = SecurityData.SelectUser(dbContext, userName);
     if (user == null)
     {
         throw new ErrorException("UserNotExist", userName);
     }
     return user;
 }
Example #12
0
 /// <summary>
 /// 获取数据库对象
 /// </summary>
 /// <param name="dbContext">DbContext</param>
 /// <param name="roleName">角色名</param>
 /// <returns></returns>
 public static SecurityRole GetSecurityRole(WarehouseEntryDbContext dbContext, string roleName)
 {
     SecurityRole role = SecurityData.SelectRole(dbContext, roleName);
     if (role == null)
     {
         throw new ErrorException("RoleNotExist", roleName);
     }
     return role;
 }