Ejemplo n.º 1
0
        /// <summary>
        /// Gets granted permissions for a role.
        /// </summary>
        /// <param name="role">Role</param>
        /// <returns>List of granted permissions</returns>
        public virtual async Task <IReadOnlyList <Permission> > GetGrantedPermissionsAsync(TRole role)
        {
            var permissionList = new List <Permission>();

            foreach (var permission in _permissionManager.GetAllPermissions())
            {
                if (await IsGrantedAsync(role.Id, permission))
                {
                    permissionList.Add(permission);
                }
            }

            return(permissionList);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets granted permissions for a user.
        /// </summary>
        /// <param name="user">Role</param>
        /// <returns>List of granted permissions</returns>
        public virtual async Task <IReadOnlyList <Permission> > GetGrantedPermissionsAsync(TUser user, long?branchId)
        {
            var permissionList = new List <Permission>();

            foreach (var permission in _permissionManager.GetAllPermissions())
            {
                if (await IsGrantedAsync(user.Id, permission, branchId))
                {
                    permissionList.Add(permission);
                }
            }

            return(permissionList);
        }
Ejemplo n.º 3
0
        public async Task <object> GetPermissionsByRole(OneParam param)
        {
            var rolePermissions = await _roleManager.GetGrantedPermissionsAsync(param.Id);

            var allPermissons   = _permissionManager.GetAllPermissions().ToList();
            var permissionGroup = new List <PermissionGroupDto>();

            foreach (var item in allPermissons.Where(m => m.Children.Count() > 0))
            {
                PermissionGroupDto tempData = item.MapTo <PermissionGroupDto>();
                tempData.PermissionGroups = item.Children.ToList().MapTo <List <PermissionListDto> >();
                permissionGroup.Add(tempData);
            }
            return(new { allPermissons = permissionGroup, rolePermissions = rolePermissions.Select(m => m.Name).ToList() });
        }
Ejemplo n.º 4
0
        public ActionResult EditPermission(int id, int parentId = 0)
        {
            var           AllList = _permissionManager.GetAllPermissions();
            SysPermission right   = AllList.FirstOrDefault(p => p.PermissionId == id) ?? new SysPermission
            {
                LinkUrl        = "",
                PermissionName = "",
                PermissionCode = "",
                Description    = ""
            };

            ViewBag.parentId  = id == 0 ? parentId : right.ParentId;
            ViewBag.allrights = AllList;
            return(View(right));
        }
 protected IList <Permission> GetPermissions(List <string> permissionNames)
 {
     return(_permissionManager
            .GetAllPermissions()
            .Where(p => permissionNames.Contains(p.Name))
            .ToList());
 }
        public string GetAuthorizationScript()
        {
            var allPermission      = _permissionManager.GetAllPermissions().Select(p => p.Name).ToList();
            var grantedPermissions =
                AbpSession.UserId.HasValue
                    ? _permissionManager.GetGrantedPermissions(AbpSession.UserId.Value).Select(p => p.Name).ToArray()
                    : new string[0];


            var script = new StringBuilder();

            script.AppendLine("(function(){");

            script.AppendLine();

            script.AppendLine("    abp.auth = abp.auth || {};");

            script.AppendLine();

            AppendPermissionList(script, "allPermissions", allPermission);

            script.AppendLine();

            AppendPermissionList(script, "grantedPermissions", grantedPermissions);

            script.AppendLine();
            script.Append("})();");

            return(script.ToString());
        }
Ejemplo n.º 7
0
 public GetPermissionsOutput GetPermissions(GetPermissionsInput input)
 {
     return(new GetPermissionsOutput
     {
         Permissions = _permissionManager.GetAllPermissions()
     });
 }
        /// <summary>
        /// 取得所有权限资源
        /// </summary>
        public ListResultDto <PermissionOutput> GetAllPermissions()
        {
            var permissions = _permissionManager.GetAllPermissions()
                              .MapTo <List <PermissionOutput> >();

            return(new ListResultDto <PermissionOutput>(permissions));
        }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        public void UpNavigationMenusProvider(SysMenus model = null)
        {
            #region 更新全局模块列表(主要解决运行时模块列表没有更新的情况)
            _navigationManager.MainMenu.Items.Clear();
            _navigationMenusProvider.CreateMenuItemDefinition(_navigationManager.MainMenu);
            #endregion
            //新增,编辑 更新基础模块的同时添加新加的授权信息
            //删除 此处为null直接更新原有菜单信息
            if (model == null)
            {
                return;
            }
            #region  更新全局授权列表(主要解决运行时授权列表没有更新的情况)
            //添加授权模块
            Permission currentPermission = _permissionManager.GetPermissionOrNull(model.PermissionName.ToLower());
            if (currentPermission == null)
            {
                IReadOnlyList <Permission> permissionList = _permissionManager.GetAllPermissions();
                currentPermission = permissionList[0].CreateChildPermission(model.PermissionName.ToLower(), model.MenuDisplayName.ToLocalizable());
            }
            //添加授权动作
            foreach (var action in model.SysMenuActions)
            {
                if (!string.IsNullOrEmpty(action.PermissionName) && _permissionManager.GetPermissionOrNull(action.PermissionName.ToLower()) == null)
                {
                    currentPermission.CreateChildPermission(action.PermissionName.ToLower(), action.ActionDisplayName.ToLocalizable());
                }
            }

            #endregion
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Prohibits all permissions for a user.
 /// </summary>
 /// <param name="user">User</param>
 public async Task ProhibitAllPermissionsAsync(User user)
 {
     foreach (var permission in _permissionManager.GetAllPermissions())
     {
         await ProhibitPermissionAsync(user, permission);
     }
 }
Ejemplo n.º 11
0
        public virtual async Task <List <PermissionDto> > GetGrantedCurrentPermissionsAsync()
        {
            //var a=AbpClaimTypes.UserId;
            var userId         = AbpSession.UserId.Value;
            var permissionList = new List <PermissionDto>();

            //var permissionDeatilList = new List<string>();
            foreach (var permission in _permissionManager.GetAllPermissions())
            {
                if (await _userManager.IsGrantedAsync(userId, permission))
                {
                    //permissionDeatilList.Add(permission.ToString());
                    permissionList.Add(Mapper.Map <PermissionDto>(permission));
                }
            }
            return(permissionList);
        }
Ejemplo n.º 12
0
        public async Task GrantAllUserPermissionsAsync(Role role)
        {
            var permissions =
                _permissionManager.GetAllPermissions(MultiTenancySides.Tenant).Where(p => !p.Name.StartsWith(SpartanPermissionNames.AgentManages)
                                                                                     );

            await SetGrantedPermissionsAsync(role, permissions);
        }
Ejemplo n.º 13
0
 public RolePermissionsService(IRoleService roleService,
     IAuthorizer authorizer,
     IPermissionManager permissionManager,
     IEventManager eventManager)
 {
     _roleService = roleService;
     _authorizer = authorizer;
     _permissionManager = permissionManager;
     _eventManager = eventManager;
     _allPermissionsCache = _permissionManager.GetAllPermissions();
 }
Ejemplo n.º 14
0
        public async Task UpdateRolePermissions(UpdateRolePermissionsInput input)
        {
            var role = await _roleManager.GetRoleByIdAsync(input.RoleId);

            var grantedPermissions = _permissionManager
                                     .GetAllPermissions()
                                     .Where(p => input.GrantedPermissionNames.Contains(p.Name))
                                     .ToList();

            await _roleManager.SetGrantedPermissionsAsync(role, grantedPermissions);
        }
        public async Task <dynamic> GetUserPermissions(IdInput <long> input)
        {
            var user = await userManager.GetUserByIdAsync(input.Id);

            var permissions = await userManager.GetGrantedPermissionsAsync(user);

            var list = new List <dynamic>();

            foreach (Permission permission in permissionManager.GetAllPermissions())
            {
                var data = new
                {
                    permission.Name,
                    DisplayName        = permission.DisplayName.Localize(localizationContext),
                    ParentName         = permission.Parent == null ? "无" : permission.Parent.Name,
                    IsGrantedByDefault = permissions.Contains(permission) || permission.IsGrantedByDefault
                };
                list.Add(data);
            }
            return(list);
        }
Ejemplo n.º 16
0
        public async Task <List <Permission> > GetAllPermissionAsync()
        {
            var permissions = await _roleManager.GetGrantedPermissionsAsync(_roleRepository.GetAll().FirstOrDefault());

            var list = _permissionManager.GetAllPermissions();
            var data = await _sysPermissionRepository.GetAll().ToListAsync();

            if (list.Count() != data.Count())
            {
                throw new AbpException("Error!!!");
            }
            return(data);
        }
        public async Task AddOrUpdate(EditRoleDto roleDto)
        {
            Role role = new Role();

            if (roleDto.Id.HasValue)
            {
                role = await roleManager.GetRoleByIdAsync((int)roleDto.Id);
            }
            role.Name      = roleDto.Name;
            role.IsDefault = roleDto.IsDefault;
            var grantedPermissions = permissionManager.GetAllPermissions().Where(p => ((JArray)roleDto.Permissions).ToObject <List <string> >().Contains(p.Name)).ToList();
            await roleManager.SetGrantedPermissionsAsync(role, grantedPermissions);

            if (roleDto.Id.HasValue)
            {
                await roleManager.UpdateAsync(role);
            }
            else
            {
                await roleManager.CreateAsync(role);
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// 获取授权
        /// </summary>
        /// <param name="list">所有授权对象</param>
        /// <param name="permissionName">授权名称</param>
        /// <returns></returns>
        public Permission GetPermissionsOrNull(string permissionName)
        {
            IReadOnlyList <Permission> allPermissionList = _permissionManager.GetAllPermissions();

            foreach (Permission item in allPermissionList)
            {
                Permission p = GetPermissionsOrNull(item, permissionName);
                if (p != null)
                {
                    return(p);
                }
            }
            return(null);
        }
Ejemplo n.º 19
0
        public List <PermissionDto> GetGrantedPermissions(int userId)
        {
            var grantedPermissions = new List <PermissionDto>();
            var allPermissions     = _permissionManager.GetAllPermissions();

            foreach (var permission in allPermissions)
            {
                if (IsGranted(userId, permission.Name))
                {
                    permission.Granted = true;
                    grantedPermissions.Add(permission);
                }
            }
            return(grantedPermissions);
        }
Ejemplo n.º 20
0
        // [InlineData("TestRole4", "TestRole4_DisplayName", "4_discription", "NoPermissionName")]
        public async Task CreateAsync_Test(string name, string displayname, string description, params string[] grantedPerssionNames)
        {
            var permissionName = _permissionManager.GetAllPermissions().FirstOrDefault().Name;
            var roleInput      = new RoleCreateInput()
            {
                Name               = name,
                DisplayName        = displayname,
                Description        = description,
                GrantedPermissions = grantedPerssionNames?.ToList()
            };

            var result = await _roleAppService.CreateAsync(roleInput);

            Assert.True(result == null);
        }
Ejemplo n.º 21
0
        /// <inheritdoc/>
        public async Task <string> GetScriptAsync()
        {
            Logger.Info("StartGetScript1");
            var allPermissionNames = _permissionManager.GetAllPermissions(false).Select(p => p.Name).ToList();

            Logger.Info("StartGetScript2");
            var grantedPermissionNames = new List <string>();

            if (AbpSession.UserId.HasValue)
            {
                using (var userManagerWrapper = IocManager.Instance.ResolveAsDisposable <UserManager>())
                {
                    var grantedPermissions = await userManagerWrapper.Object.GetGrantedPermissionsAsync(AbpSession.UserId.Value);

                    grantedPermissionNames = grantedPermissions.Select(o => o.Name).ToList();
                }
                //foreach (var permissionName in allPermissionNames)
                //{
                //    if (await PermissionChecker.IsGrantedAsync(permissionName))
                //    {
                //        grantedPermissionNames.Add(permissionName);
                //    }
                //}
            }
            Logger.Info("StartGetScript3");
            var script = new StringBuilder();

            script.AppendLine("(function(){");

            script.AppendLine();

            script.AppendLine("    abp.auth = abp.auth || {};");

            script.AppendLine();

            AppendPermissionList(script, "allPermissions", allPermissionNames);

            script.AppendLine();

            AppendPermissionList(script, "grantedPermissions", grantedPermissionNames);

            script.AppendLine();
            script.Append("})();");

            return(script.ToString());
        }
Ejemplo n.º 22
0
        //
        // GET: /Login/
        //[OutputCache(Duration = 100)]
        public ActionResult Index()
        {
            var    defaultLanguage = System.Configuration.ConfigurationManager.AppSettings["defaultLanguage"];
            string backUrl         = "";

            if (this.SessionExt()["backUrl"] != null)
            {
                backUrl = this.SessionExt()["backUrl"].ToString();
            }
            if (!string.IsNullOrEmpty(Request.QueryString["backUrl"]))
            {
                backUrl = Request.QueryString["backUrl"];
            }
            if (backUrl == "/")
            {
                backUrl = "/" + defaultLanguage;
            }
            this.SessionExt()["backUrl"] = string.IsNullOrEmpty(backUrl)
                ? Url.RetechAction("Index", "Home", new { culture = CultureInfo.CurrentCulture.ToString() })
                : backUrl;

            #region remember password

            //TempData["userName"] = "";
            this.SessionExt()["password"] = "";
            this.SessionExt()["remember"] = 0;
            HttpCookie httpCookie = Request.Cookies["RetechWing_User"];
            if (httpCookie != null)
            {
                this.SessionExt()["remember"] = 1;
                this.SessionExt()["userName"] = httpCookie["username"];
                this.SessionExt()["password"] = Base64Provider.DecodeBase64String(httpCookie["password"]);
            }

            #endregion
            var ps          = _permissionManager.GetAllPermissions();
            var psUpdateSql = new StringBuilder();
            foreach (var permission in ps)
            {
                psUpdateSql.AppendFormat(@"UPDATE Sys_Permission SET AreaName='{0}' 
WHERE PermissionName='{1}'", permission.AreaName, permission.PermissionName);
                psUpdateSql.AppendLine();
            }
            ViewBag.sql = psUpdateSql.ToString();
            return(View());
        }
Ejemplo n.º 23
0
        public override async Task <RoleGetUpdateOutput> GetUpdateAsync(EntityDto <int> input)
        {
            var role = await _roleManager.GetRoleByIdAsync(input.Id);

            var roleUpdate = ObjectMapper.Map <RoleUpdateDto>(role);

            var permissions = _permissionManager.GetAllPermissions();

            var grantedPermission = await _roleManager.GetGrantedPermissionsAsync(input.Id);

            return(new RoleGetUpdateOutput()
            {
                Role = roleUpdate,
                Permissions = ObjectMapper.Map <List <PermissionGetViewOutput> >(permissions),
                GrantedPermissionNames = grantedPermission.Select(m => m.Name).ToList()
            });
        }
Ejemplo n.º 24
0
        public virtual async Task <List <Permission> > GetGrantedAllPermissionsAsync(long userId)
        {
            var permissionList = new List <Permission>();

            foreach (var permission in _permissionManager.GetAllPermissions())
            {
                if (await _userManager.IsGrantedAsync(userId, permission))
                {
                    var temp = permission;
                    permissionList.Add(Mapper.Map <Permission>(permission));
                }
            }

            return(permissionList);
            //.ToArray<Permission[]>();
            //https://www.cnblogs.com/dean-Wei/p/3150553.html
            //http://www.voidcn.com/article/p-wpdeckgc-btn.html
        }
Ejemplo n.º 25
0
        public async Task <IReadOnlyList <Permission> > GetGrantedPermissionsAsync(int roleId)
        {
            var role = await FindByIdAsync(roleId);

            if (role == null)
            {
                throw new AbpAuthorizationException("There is no role with id = " + roleId);
            }

            var permissionList = new List <Permission>();

            foreach (var permission in _permissionManager.GetAllPermissions())
            {
                if (await HasPermissionInternalAsync(role, permission))
                {
                    permissionList.Add(permission);
                }
            }

            return(permissionList);
        }
Ejemplo n.º 26
0
        public async Task <string> GetScriptAsync()
        {
            var allPermissionNames     = _permissionManager.GetAllPermissions(false).Select(p => p.Name).ToList();
            var grantedPermissionNames = new List <string>();

            if (AbpSession.UserId.HasValue)
            {
                foreach (var permissionName in allPermissionNames)
                {
                    if (await PermissionChecker.IsGrantedAsync(AbpSession.UserId.Value, permissionName))
                    {
                        grantedPermissionNames.Add(permissionName);
                    }
                }
            }

            var script = new StringBuilder();

            script.AppendLine("(function(){");

            script.AppendLine();

            script.AppendLine("    abp.auth = abp.auth || {};");

            script.AppendLine();

            AppendPermissionList(script, "allPermissions", allPermissionNames);

            script.AppendLine();

            AppendPermissionList(script, "grantedPermissions", grantedPermissionNames);

            script.AppendLine();

            script.Append("})();");

            return(script.ToString());
        }
Ejemplo n.º 27
0
        public async Task UpdateRolePermissions_Test()
        {
            LoginAsHostAdmin();

            await CreateFakeRole();


            await UsingDbContextAsync(async dbContext =>
            {
                var role = await GetFakeRole(dbContext);

                var permissions = _permissionManager.GetAllPermissions();

                var permissionStrings = permissions.Select(a => a.Name).ToList();


                await _roleAppService.UpdateRolePermissions(new UpdateRolePermissionsInput()
                {
                    GrantedPermissionNames = permissionStrings,
                    RoleId = role.Id
                });
            });
        }
Ejemplo n.º 28
0
        private async Task <UserAuthConfigDto> GetUserAuthConfig()
        {
            var config = new UserAuthConfigDto();

            var allPermissionNames     = _permissionManager.GetAllPermissions(false).Select(p => p.Name).ToList();
            var grantedPermissionNames = new List <string>();

            if (_Session.UserId.HasValue)
            {
                foreach (var permissionName in allPermissionNames)
                {
                    if (await _permissionChecker.IsGrantedAsync(permissionName))
                    {
                        grantedPermissionNames.Add(permissionName);
                    }
                }
            }

            config.AllPermissions     = allPermissionNames.ToDictionary(permissionName => permissionName, permissionName => "true");
            config.GrantedPermissions = grantedPermissionNames.ToDictionary(permissionName => permissionName, permissionName => "true");

            return(config);
        }
Ejemplo n.º 29
0
        public async Task <UserSpecialPermissionsInput> GetUserSpecialPermissions(long?userId)
        {
            var assignedPermissions = new List <AssignedPermission>();
            var allPermissions      = _permissionManager.GetAllPermissions().Where(a => a.Parent == null).ToList();

            if (!userId.HasValue)
            {
                return new UserSpecialPermissionsInput()
                       {
                           AssignedPermissions = assignedPermissions
                       }
            }
            ;
            var user = await UserManager.GetUserByIdAsync(userId.Value);

            var userPermissions = (await UserManager.GetGrantedPermissionsAsync(user)).ToList();

            assignedPermissions = CheckPermissions(allPermissions, userPermissions).ToList();
            return(new UserSpecialPermissionsInput()
            {
                UserId = userId,
                AssignedPermissions = assignedPermissions
            });
        }
Ejemplo n.º 30
0
        public ListResultDto <PermissionGetViewOutput> GetViewAll(PermissionGetViewInput input)
        {
            var permissions = _permissionManager.GetAllPermissions();

            return(ObjectMapper.Map <ListResultDto <PermissionGetViewOutput> >(permissions));
        }