예제 #1
0
        /// <summary>
        /// Seeds the default data.
        /// </summary>
        private void Seed()
        {
            SaveChanges();

            // Make sure we have a SysAdmin role
            var role = Roles.FirstOrDefault(r => r.Name == "SysAdmin");

            if (role == null)
            {
                role = new Role
                {
                    Id             = Guid.NewGuid(),
                    Name           = "SysAdmin",
                    NormalizedName = "SYSADMIN"
                };
                Roles.Add(role);
            }

            // Make sure our SysAdmin role has all of the available claims
            //foreach (var claim in Piranha.Security.Permission.All())
            foreach (var permission in App.Permissions.GetPermissions())
            {
                var roleClaim = RoleClaims.FirstOrDefault(c => c.RoleId == role.Id && c.ClaimType == permission.Name && c.ClaimValue == permission.Name);
                if (roleClaim == null)
                {
                    RoleClaims.Add(new IdentityRoleClaim <Guid>
                    {
                        RoleId     = role.Id,
                        ClaimType  = permission.Name,
                        ClaimValue = permission.Name
                    });
                }
            }
            SaveChanges();
        }
예제 #2
0
        public override async Task AddClaimAsync(IdentityRole <TKey> role, Claim claim, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            role.ThrowIfNull(nameof(role));
            claim.ThrowIfNull(nameof(claim));

            RoleClaims ??= (await RoleClaimsTable.GetClaimsAsync(role.Id)).ToList();
            RoleClaims.Add(CreateRoleClaim(role, claim));
        }
예제 #3
0
        /// <summary>
        /// Adds the <paramref name="claim"/> given to the specified <paramref name="role"/>.
        /// </summary>
        /// <param name="role">The role to add the claim to.</param>
        /// <param name="claim">The claim to add to the role.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
        /// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
        public virtual Task AddClaimAsync(Role role, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
        {
            ThrowIfDisposed();
            if (role == null)
            {
                throw new ArgumentNullException(nameof(role));
            }
            if (claim == null)
            {
                throw new ArgumentNullException(nameof(claim));
            }

            RoleClaims.Add(CreateRoleClaim(role, claim));
            return(Task.FromResult(false));
        }
예제 #4
0
        private async Task <IdentityResult> UpdateClaimsAsync(LondonTravelUser user, ExternalLoginInfo info)
        {
            bool commitUpdate = false;

            if (user.RoleClaims == null)
            {
                user.RoleClaims = new List <LondonTravelRole>();
                commitUpdate    = true;
            }

            foreach (var claim in info.Principal.Claims)
            {
                bool hasClaim = user?.RoleClaims
                                .Where((p) => p.ClaimType == claim.Type)
                                .Where((p) => p.Issuer == claim.Issuer)
                                .Where((p) => p.Value == claim.Value)
                                .Where((p) => p.ValueType == claim.ValueType)
                                .Any() == true;

                if (!hasClaim)
                {
                    user !.RoleClaims.Add(LondonTravelRole.FromClaim(claim));
                    commitUpdate = true;
                }
            }

            if (commitUpdate)
            {
                var result = await _userManager.UpdateAsync(user);

                if (result.Succeeded)
                {
                    _telemetry.TrackClaimsUpdated(user.Id !);
                }

                return(result);
            }
            else
            {
                return(IdentityResult.Success);
            }
        }
예제 #5
0
        public virtual async Task <IdentityResult> AddClaimAsync(IdentityRole role, Claim claim, CancellationToken cancellationToken = default)
        {
            ThrowIfDisposed();
            role.CheakArgument();
            claim.CheakArgument();
            var f2 = await DbEntitySet.AnyAsync(d => d.Id.Equals(role.Id));

            if (f2)
            {
                var flag = await RoleClaims.AnyAsync(a => a.ClaimType.Equals(claim.Type) &&
                                                     a.ClaimValue.Equals(claim.Value) && a.RoleId.Equals(role.Id));

                if (!flag)
                {
                    RoleClaims.Add(CreateRoleClaim(role, claim));
                    return(await SaveChangesAsync(cancellationToken));
                }
            }
            return(IdentityResult.Failed("已经存在该权限"));
        }