Ejemplo n.º 1
0
        public static void UpdateRoleClaims(this model.Role dbRole, Model.Role role)
        {
            var securityClaimIds = role.SecurityClaims.Select(o => o.SecurityClaimId).ToArray();

            var remove = (from c in dbRole.SecurityClaims
                          where !securityClaimIds.Contains(c.SecurityClaimId)
                          select c).ToList();

            remove.ForEach(o => dbRole.SecurityClaims.Remove(o));

            var add = (from c in role.SecurityClaims
                       where !dbRole.SecurityClaims.Any(o => o.SecurityClaimId == c.SecurityClaimId)
                       select c).ToList();

            add.ForEach(o =>
            {
                dbRole.SecurityClaims.Add(new model.RoleSecurityClaim()
                {
                    SecurityClaimId = o.SecurityClaimId,
                    Value           = o.Value
                });
            });

            var update = role.SecurityClaims.Except(add).ToList();

            update.ForEach(claim =>
            {
                var match   = dbRole.SecurityClaims.Single(o => o.SecurityClaimId == claim.SecurityClaimId);
                match.Value = claim.Value;
            });
        }
Ejemplo n.º 2
0
        public static Model.Role MapToVm(this model.Role value)
        {
            if (value == null)
            {
                return(null);
            }

            return(new Model.Role()
            {
                AssignedUserCount = value.Users.Count(),
                SecurityClaims = value.SecurityClaims.Select(o => o.MapToVm()).ToArray(),
                CreatedOn = value.CreatedOn,
                CreatedByUser = new Model.User()
                {
                    UserId = value.CreatedByUser?.UserId ?? 0,
                    DisplayName = value.CreatedByUser?.DisplayName
                },
                Enabled = value.Enabled,
                RoleId = value.RoleId,
                LastUpdatedByUser = new Model.User()
                {
                    UserId = value.LastUpdatedByUser?.UserId ?? 0,
                    DisplayName = value.LastUpdatedByUser?.DisplayName
                },
                LastUpdatedOn = value.LastUpdatedOn,
                Name = value.Name,
                Parent = value.Parent?.MapToVm(),
                ParentRoleId = value.ParentRoleId
            });
        }
Ejemplo n.º 3
0
        public async Task <Role> CreateRole(Role role, ClaimsPrincipal currentUser)
        {
            var exists = this.db.Role.Any(o => o.RoleId == role.RoleId);

            if (exists)
            {
                throw new ArgumentException($"A role with unique key '{role.RoleId}' already exists");
            }

            if (string.IsNullOrWhiteSpace(role.ParentRoleId))
            {
                throw new ArgumentException($"A custom role must inherit from a system role");
            }

            if (role.ParentRoleId == RoleTypes.Admin)
            {
                throw new ArgumentException($"You cannot inherit from {RoleTypes.Admin} role");
            }

            var dbRole = new model.Role()
            {
                Enabled      = role.Enabled,
                RoleId       = role.RoleId,
                Name         = role.Name,
                ParentRoleId = role.ParentRoleId,
                CreatedBy    = currentUser.UserId(),
                CreatedOn    = DateTime.UtcNow
            };

            dbRole.UpdateRoleClaims(role);

            this.db.Role.Add(dbRole);

            await this.db.SaveChangesAsync();

            return(dbRole.MapToVm());
        }