public IActionResult ShowRoleTree()
        {
            var response = ResponseModelFactory.CreateInstance;

            using (_dbContext)
            {
                var query = _dbContext.SubdistrictCommunity.OrderBy(x => x.Towns).ThenBy(x => x.Vname).ThenBy(x => x.Resregion);
                var list1 = query.ToList();
                var tree  = new RoleTree();
                tree.Children      = new List <RoleTreeNode>();
                tree.Title         = "全部";
                tree.Selected      = false;
                tree.Expand        = true;
                tree.Disabled      = false;
                tree.Checked       = false;
                tree.Indeterminate = false;
                var tlist = query.GroupBy(x => x.Towns).Select(x => new RoleTreeNode
                {
                    Title         = x.Key,
                    Selected      = false,
                    Expand        = false,
                    Disabled      = false,
                    Checked       = false,
                    Indeterminate = false,
                    Children      = ToSetVlist(x.Key, list1),
                }).ToList();
                tree.Children = tlist;
                response.SetData(tree);

                return(Ok(response));
            }
        }
Esempio n. 2
0
        public async Task <MessageModel <RoleTree> > GetRoleTree()
        {
            var data  = new MessageModel <RoleTree>();
            var roles = await _roleServices.Query(d => d.IsDeleted == false);

            var roleTrees = (from child in roles
                             where child.IsDeleted == false
                             orderby child.OrderSort
                             select new RoleTree
            {
                value = child.Id,
                label = child.Name,
                pid = child.Pid,
                order = child.OrderSort,
            }).ToList();
            RoleTree rootRoot = new RoleTree
            {
                value = 0,
                pid   = 0,
                label = "根节点"
            };

            RecursionHelper.LoopToAppendChildrenT(roleTrees, rootRoot);

            data.success = true;
            if (data.success)
            {
                data.response = rootRoot;
                data.msg      = "获取成功";
            }

            return(data);
        }
Esempio n. 3
0
        public async Task <MessageModel <List <RoleTree> > > GetCurrentUserRoleTree(bool hasCurrentRole = false)
        {
            var userRoles = await _userRoleServices.Query(it => it.UserId == _user.ID);

            var roleIds = userRoles.Select(it => it.RoleId).ToList();
            var data    = new MessageModel <List <RoleTree> >();
            //超级管理员例外,可以操作所有角色。
            var allRoles = await _roleServices.Query(d => d.IsDeleted == false);

            var roleTrees = (from child in allRoles
                             where child.IsDeleted == false
                             orderby child.OrderSort
                             select new RoleTree
            {
                value = child.Id,
                label = child.Name,
                pid = child.Pid,
                order = child.OrderSort,
            }).ToList();
            var roleTreeList = new List <RoleTree>();

            if (roleIds.Contains(1))
            {
                roleIds = allRoles.Where(it => it.Pid == 0 && it.Id != 1).Select(it => it.Id).ToList();
            }
            roleIds.ForEach(id =>
            {
                RoleTree rootRoot = new RoleTree
                {
                    value = id,
                    pid   = 0,
                    label = allRoles.Find(it => it.Id == id).Name
                };
                RecursionHelper.LoopToAppendChildrenT(roleTrees, rootRoot);
                roleTreeList.Add(rootRoot);
            });


            data.success = true;
            if (data.success)
            {
                data.response = roleTreeList;
                data.msg      = "获取成功";
            }

            return(data);
        }
Esempio n. 4
0
        public static RoleTree GetRoleTree(IEnumerable <string> serviceRoles, IEnumerable <MethodInfoData> methods)
        {
            var tree = new RoleTree
            {
                Roles       = serviceRoles,
                ownerRoles  = new RolesForOwner[0],
                methodRoles = new RolesForMethod[0]
            };

            foreach (var method in methods)
            {
                if (method.isInDataManager)
                {
                    var ownerRoles = tree.ownerRoles.Where(r => r.ownerType == method.ownerType).FirstOrDefault();
                    if (ownerRoles == null)
                    {
                        ownerRoles = new RolesForOwner
                        {
                            ownerType   = method.ownerType,
                            Roles       = new string[0],
                            methodRoles = new RolesForMethod[0]
                        };
                        //the override attribute replaces all other roles
                        var override_roles = method.ownerType.GetCustomAttribute <OverrideAuthorizeAttribute>(false);
                        if (override_roles != null)
                        {
                            ownerRoles.IsRolesOverride = true;
                            ownerRoles.Roles           = override_roles.Roles.Select(a => a.Trim()).Distinct().ToArray();
                        }
                        else
                        {
                            //allow unauthenticated access
                            var passthrough_attrs =
                                method.ownerType.GetCustomAttributes(typeof(AllowAnonymousAttribute), false)
                                .OfType <AllowAnonymousAttribute>();
                            if (passthrough_attrs.Any())
                            {
                                ownerRoles.Roles = new[] { ALLOW_ANONYMOUS }
                            }
                            ;
                            else
                            {
                                ownerRoles.Roles = GetRolesFromType(method.ownerType);
                            }
                        }
                        tree.ownerRoles = tree.ownerRoles.Concat(new[] { ownerRoles });
                    }

                    var method_roles =
                        ownerRoles.methodRoles.Where(m => m.MethodName == method.methodInfo.Name).FirstOrDefault();
                    if (method_roles == null)
                    {
                        method_roles           = GetRolesForMethod(method);
                        ownerRoles.methodRoles = ownerRoles.methodRoles.Concat(new[] { method_roles });
                    }
                }
                else
                {
                    var method_roles =
                        tree.methodRoles.Where(m => m.MethodName == method.methodInfo.Name).FirstOrDefault();
                    if (method_roles == null)
                    {
                        method_roles     = GetRolesForMethod(method);
                        tree.methodRoles = tree.methodRoles.Concat(new[] { method_roles });
                    }
                }
            }

            return(tree);
        }
		public RoleTree Get(RoleTree roleTree)
		{
			IAuthSession session = this.GetSession();
			UserEntity userEntity = SessionUtility.GetSessionUserEntity(session);

			//Get the rootparent entity
			RoleEntity entity = GenericRepository.GetItemTopById<RoleEntity>(AuthAdminDatabase, RolesCollection, userEntity.Roles[0]);

			if (entity == null || entity.DateTimeDeleted > DateTime.MinValue)
				return null;

			roleTree.TreeItems = new List<TreeItem>();
			TreeItem treeItem = new TreeItem();
			SetTreeItem(treeItem, entity);
			roleTree.TreeItems.Add(treeItem);

			return roleTree;
		}