コード例 #1
0
        /// <summary>
        /// 根据菜单url查询某个所拥有的角色
        /// 从NavigatorRole表查
        /// 从Navigators -> GroupNavigatorRole -> Role查查询某个用户所拥有的角色
        /// </summary>
        /// <param name="url"></param>
        /// <param name="appId"></param>
        /// <returns></returns>
        public override IEnumerable <string> RetrievesByUrl(string url, string appId)
        {
            var menu = DbManager.Menus.Find(md => md.Url.StartsWith(url)).FirstOrDefault();
            var ret  = RoleHelper.Retrieves().Where(md => md.Menus.Any(m => m == menu.Id) && md.Apps.Any(m => m.Equals(appId, StringComparison.OrdinalIgnoreCase))).Select(m => m.RoleName).ToList();

            if (!ret.Any(r => r.Equals("Administrators", StringComparison.OrdinalIgnoreCase)))
            {
                ret.Add("Administrators");
            }
            return(ret);
        }
コード例 #2
0
ファイル: Menu.cs プロジェクト: xingx001/BootstrapAdmin
        /// <summary>
        /// 通过当前用户名获得所有菜单
        /// </summary>
        /// <param name="userName">当前登录的用户名</param>
        /// <returns></returns>
        public override IEnumerable <BootstrapMenu> RetrieveAllMenus(string userName)
        {
            var user = UserHelper.RetrieveUserByUserName(userName) as User;

            if (user == null)
            {
                return(Enumerable.Empty <BootstrapMenu>());
            }

            var roles  = RoleHelper.Retrieves();
            var groups = DbManager.Groups.Find(FilterDefinition <Group> .Empty).ToList();

            // 通过用户获取 组列表相关联的角色列表
            var userRoles = user.Groups.Aggregate(user.Roles.ToList(), (r, g) =>
            {
                var groupRoles = groups.FirstOrDefault(group => group.Id == g)?.Roles;
                if (groupRoles != null)
                {
                    r.AddRange(groupRoles);
                }
                return(r);
            }).Distinct().ToList();

            var allRoles = roles.Where(r => userRoles.Any(rl => rl == r.Id));
            var menus    = DbManager.Menus.Find(FilterDefinition <BootstrapMenu> .Empty).ToList();

            // check administrators
            if (!allRoles.Any(r => r.RoleName.Equals("Administrators", StringComparison.OrdinalIgnoreCase)))
            {
                menus = menus.Where(m => allRoles.Any(r => r.Menus.Any(rm => rm == m.Id))).ToList();
            }

            var dicts = DictHelper.RetrieveDicts().Where(m => m.Category == "菜单");

            menus.ForEach(m =>
            {
                m.CategoryName = dicts.FirstOrDefault(d => d.Code == m.Category)?.Name ?? "";
                if (m.ParentId != "0")
                {
                    m.ParentName = menus.FirstOrDefault(p => p.Id == m.ParentId)?.Name ?? "";
                }
            });

            return(menus);
        }
コード例 #3
0
        /// <summary>
        /// 根据指定用户名获得授权应用程序集合
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public override IEnumerable<string> RetrievesByUserName(string userName)
        {
            var ret = new List<string>();
            var user = UserHelper.RetrieveUserByUserName(userName) as User;
            if (user != null)
            {
                var roles = RoleHelper.Retrieves();

                // check administrators
                if (roles.Any(r => r.RoleName.Equals("Administrators", StringComparison.OrdinalIgnoreCase)))
                {
                    ret.AddRange(RetrieveApps().Select(kv => kv.Key));
                }
                else
                {
                    roles.Where(r => user.Roles.Any(role => role == r.Id)).ToList().ForEach(r => ret.AddRange(r.Apps));
                }
            }
            return ret;
        }
コード例 #4
0
ファイル: Role.cs プロジェクト: tim-huang61/BootstrapAdmin
        /// <summary>
        /// 通过指定登录名获取授权角色名称数据集合
        /// </summary>
        /// <param name="userName">登录名</param>
        /// <returns></returns>
        public override IEnumerable <string> RetrievesByUserName(string userName)
        {
            var user = UserHelper.RetrieveUserByUserName(userName) as User;

            return(RoleHelper.Retrieves().Where(r => user.Roles.Any(ur => ur == r.Id)).Select(r => r.RoleName));
        }