/// <summary>
        /// 获取角色所有的访问控制详情
        /// </summary>
        /// <returns></returns>
        public (IQueryable <ClientApp>, IQueryable <UserInfo>, IQueryable <UserGroupRole>, List <Role>, List <Permission>, List <Control>, List <Menu>) Details(Role role)
        {
            DataContext                context     = BaseDal.GetDataContext();
            IQueryable <ClientApp>     apps        = new ClientAppBll().LoadEntities(a => a.Roles.Any(r => r.Id == role.Id));
            IQueryable <UserInfo>      users       = new UserInfoBll().LoadEntities(u => u.Role.Any(r => r.Id == role.Id));
            IQueryable <UserGroupRole> groups      = new UserGroupRoleBll().LoadEntities(g => g.RoleId == role.Id);
            List <Control>             controls    = new List <Control>();
            List <Menu>                menus       = new List <Menu>();
            List <Permission>          permissions = new List <Permission>();
            List <Role>                roles       = new List <Role>();

            var rids = GetParentIdById(role.Id); //拿到所有上级角色,并排除掉角色不可用的角色id

            foreach (int rid in rids)
            {
                Role r = context.Role.FirstOrDefault(o => o.Id == rid);
                if (r?.Id != role.Id)
                {
                    roles.Add(r);
                }
                r?.Permission.ForEach(p =>
                {
                    //2.3 拿到所有上级权限
                    int[] pids = context.Database.SqlQuery <int>("exec sp_getParentPermissionIdByChildId " + p.Id).ToArray(); //拿到所有上级权限
                    foreach (int s in pids)
                    {
                        Permission permission = context.Permission.FirstOrDefault(x => x.Id == s);
                        permissions.Add(permission);
                        controls.AddRange(permission.Controls.Where(c => c.IsAvailable));
                        menus.AddRange(permission.Menu.Where(c => c.IsAvailable));
                    }
                });
            }
            return(apps, users, groups, roles, permissions.Distinct().ToList(), controls.Distinct().ToList(), menus.Distinct().ToList());
        }
Esempio n. 2
0
        /// <summary>
        /// 获取用户组所有的访问控制详情
        /// </summary>
        /// <param name="group"></param>
        /// <param name="g"></param>
        /// <returns></returns>
        public (IQueryable <ClientApp>, IQueryable <UserInfo>, List <UserGroup>, List <UserGroupRole>, List <Permission>, List <Control>, List <Menu>) Details(UserGroup @group)
        {
            DataContext            context     = BaseDal.GetDataContext();
            IQueryable <ClientApp> apps        = new ClientAppBll().LoadEntities(a => a.UserGroup.Any(p => p.Id == group.Id));
            IQueryable <UserInfo>  users       = new UserInfoBll().LoadEntities(u => u.UserGroup.Any(g => g.Id == group.Id));
            List <UserGroup>       groups      = new List <UserGroup>();
            List <Control>         controls    = new List <Control>();
            List <Menu>            menus       = new List <Menu>();
            List <Permission>      permissions = new List <Permission>();
            List <UserGroupRole>   groupRoles  = new List <UserGroupRole>();

            //2.1 拿到所有上级用户组
            int[] gids = context.Database.SqlQuery <int>("exec sp_getParentGroupIdByChildId " + group.Id).ToArray(); //拿到所有上级用户组
            foreach (int i in gids)
            {
                UserGroup gg = context.UserGroup.FirstOrDefault(u => u.Id == i);
                if (i != group.Id)
                {
                    groups.Add(gg);
                }
                List <int> noRoleIds = gg?.UserGroupRole.Where(x => !x.HasRole).Select(x => x.Id).ToList(); //没有角色的id集合
                gg?.UserGroupRole.ForEach(ugp =>
                {
                    groupRoles.Add(ugp);
                    if (ugp.HasRole)
                    {
                        //角色可用,取并集
                        //2.2 拿到所有上级角色,并排除掉角色不可用的角色id
                        int[] rids = context.Database.SqlQuery <int>("exec sp_getParentRoleIdByChildId " + ugp.Role.Id).Except(noRoleIds).ToArray(); //拿到所有上级角色,并排除掉角色不可用的角色id
                        foreach (int r in rids)
                        {
                            Role role = context.Role.FirstOrDefault(o => o.Id == r);
                            role?.Permission.ForEach(p =>
                            {
                                //2.3 拿到所有上级权限
                                int[] pids = context.Database.SqlQuery <int>("exec sp_getParentPermissionIdByChildId " + p.Id).ToArray(); //拿到所有上级权限
                                foreach (int s in pids)
                                {
                                    Permission permission = context.Permission.FirstOrDefault(x => x.Id == s);
                                    permissions.Add(permission);
                                    controls.AddRange(permission.Controls.Where(c => c.IsAvailable));
                                    menus.AddRange(permission.Menu.Where(c => c.IsAvailable));
                                }
                            });
                        }
                    }
                    else
                    {
                        //角色不可用,取差集
                        ugp.Role.Permission.ForEach(p => controls = controls.Except(p.Controls).Where(c => c.IsAvailable).ToList());
                        ugp.Role.Permission.ForEach(p => menus    = menus.Except(p.Menu).Where(c => c.IsAvailable).ToList());
                    }
                });
            }
            return(apps, users, groups, groupRoles.Distinct().ToList(), permissions.Distinct().ToList(), controls.Distinct().ToList(), menus.Distinct().ToList());
        }
Esempio n. 3
0
 /// <summary>
 /// 在角色分配页面向该角色添加一个用户,RoleUserInfo表,UserInfo表都要新增一条记录
 /// </summary>
 /// <returns></returns>
 public int AddUser(string roleName,string userName,string realName)
 {
     CarSaleEntities context=new CarSaleEntities();
     UserInfoBll bll=new UserInfoBll();
     bll.AddUserInfo(userName, realName, "123456", " ", " ", " ", " ", " ", 1, " ");
     var user = context.UserInfo.FirstOrDefault(u => u.UserName == userName);
     var role = context.Role.FirstOrDefault(r => r.RoleName == roleName);
     RoleUserInfo newitem=new RoleUserInfo();
     newitem.RoleID = role.ID;
     newitem.UserID = user.ID;
     context.AddToRoleUserInfo(newitem);
     context.SaveChanges();
     return 1;
 }
Esempio n. 4
0
 public ActionResult UserDetail(int userid)
 {
     UserInfoBll bll=new UserInfoBll();
     var user=bll.GetUserById(userid);
     return View(user);
 }
Esempio n. 5
0
 /// <summary>
 /// 验证会员登陆
 /// </summary>
 /// <param name="userName"></param>
 /// <param name="password"></param>
 /// <returns></returns>
 public ActionResult CheckLogin(string userName, string password)
 {
     UserInfoBll bll=new UserInfoBll();
     int result=bll.CheckUser(userName, password);
     if (result==1)
     {
         CarSaleEntities context=new CarSaleEntities();
         var firstOrDefault = context.UserInfo.FirstOrDefault(u => u.UserName == userName);
         if (firstOrDefault != null)
         {
             int userid = firstOrDefault.ID;
             Session.Add("logID",userid);
         }
         return RedirectToAction("Index");
     }
     else
     {
         return RedirectToAction("LoginFaile");//跳转到登陆失败页面
     }
 }