Esempio n. 1
0
        /// <summary>
        /// Add permissions to role
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="permissions"></param>
        /// <returns></returns>
        public async Task <List <Permission> > AddPermissionsToRole(long roleId, List <Permission> permissions)
        {
            var role = await _roleRepository.GetRoleById(roleId);

            await _permissionRepository.ClearPermissionsForRole(role);

            await _permissionRepository.AddPermissionsToRole(role, permissions.Select(_mapper.Map <ApplicationPermission>).ToList());

            return(await GetPermissionsByRole(roleId));
        }
        /// <summary>
        /// Create a role
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="description"></param>
        /// <param name="claims"></param>
        /// <returns></returns>
        private async Task EnsureRoleAsync(string roleName, string description, string[] claims)
        {
            if (await _roleRepository.GetRoleByName(roleName) == null)
            {
                var role = new ApplicationRole
                {
                    Name        = roleName,
                    Description = description,
                    IsActive    = true
                };
                await _roleRepository.CreateRole(role);

                await _permissionRepository.AddPermissionsToRole(role, claims.Select(ApplicationPermissions.GetPermissionByValue).ToList());
            }
        }