Example #1
0
        public async Task AffectRoleToPrincipalOnScopeAsync(string roleName, Guid principalId, string scopeName)
        {
            Data.Role role = await this.GetEntityAsync <Data.Role>(r => r.Name == roleName);

            if (role == null)
            {
                throw new EntityNotFoundException(roleName);
            }

            Data.Scope scope = await this.GetEntityAsync <Data.Scope>(s => s.Name == scopeName);

            if (scope == null)
            {
                throw new EntityNotFoundException(scopeName);
            }

            Data.Principal principal = await this.GetEntityAsync <Data.Principal>(s => s.Id == principalId);

            if (principal == null)
            {
                throw new EntityNotFoundException($"Principal '{principalId}'");
            }

            var localAuthorization = context.ChangeTracker.Entries <Data.Authorization>()
                                     .FirstOrDefault(e => e.Entity.RoleId == role.Id &&
                                                     e.Entity.ScopeId == scope.Id &&
                                                     e.Entity.PrincipalId == principalId);
            var authorization = await this.context.Set <Data.Authorization>()
                                .FirstOrDefaultAsync(a => a.PrincipalId == principalId &&
                                                     a.RoleId == role.Id &&
                                                     a.ScopeId == scope.Id);

            if (localAuthorization != null)
            {
                localAuthorization.State = authorization == null ? EntityState.Added : EntityState.Unchanged;
            }
            else if (authorization == null)
            {
                this.context.Set <Data.Authorization>().Add(new Data.Authorization
                {
                    Role           = role,
                    Scope          = scope,
                    Principal      = principal,
                    CreationBy     = this.principalIdProvider.PrincipalId,
                    ModificationBy = this.principalIdProvider.PrincipalId
                });
            }
        }
Example #2
0
        public async Task <IGroup> CreateGroupAsync(string groupName, string parentGroupName = null)
        {
            var group = await this.GetEntityAsync <Data.Group>(r => r.Name == groupName);

            if (group == null)
            {
                var principal = new Data.Principal
                {
                    Id             = Guid.NewGuid(),
                    CreationBy     = this.principalIdProvider.PrincipalId,
                    ModificationBy = this.principalIdProvider.PrincipalId
                };
                group = new Data.Group(principal)
                {
                    Name = groupName
                };

                this.context.Set <Data.Group>().Add(group);

                if (parentGroupName != null)
                {
                    await this.CreateGroupAsync(parentGroupName);

                    var parentGoup = await this.GetEntityAsync <Data.Group>(r => r.Name == parentGroupName);

                    this.context.Set <Data.Membership>().Add(new Data.Membership
                    {
                        CreationBy     = principal.CreationBy,
                        ModificationBy = principal.ModificationBy,
                        PrincipalId    = group.Id,
                        Group          = parentGoup
                    });
                }
            }
            return(group);
        }