Esempio n. 1
0
        public async Task DeleteRightAsync(string rightName)
        {
            var right = await this.GetEntityAsync <Data.Right>(r => r.Name == rightName);

            if (right != null)
            {
                this.context.Set <Data.Right>().Remove(right);
                (await SharedQueries.GetModelModificationDateAsync(this.context)).Rights = DateTime.UtcNow;
            }
        }
Esempio n. 2
0
        public async Task DeleteRoleAsync(string roleName)
        {
            var role = await this.GetEntityAsync <Data.Role>(r => r.Name == roleName);

            if (role != null)
            {
                var roleRights = await this.context.Set <Data.RoleRight>()
                                 .Where(rr => rr.RoleId == role.Id).ToListAsync();

                this.context.Set <Data.RoleRight>().RemoveRange(roleRights);
                this.context.Set <Data.Role>().Remove(role);
                (await SharedQueries.GetModelModificationDateAsync(this.context)).Roles = DateTime.UtcNow;
            }
        }
Esempio n. 3
0
        public async Task CreateRightAsync(string rightName)
        {
            var right = await this.GetEntityAsync <Data.Right>(r => r.Name == rightName);

            if (right == null)
            {
                var rightEntity = this.context.Set <Data.Right>().Add(new Data.Right
                {
                    Name           = rightName,
                    CreationBy     = this.principalIdProvider.PrincipalId,
                    ModificationBy = this.principalIdProvider.PrincipalId
                });

                (await SharedQueries.GetModelModificationDateAsync(this.context)).Rights = DateTime.UtcNow;
            }
        }
Esempio n. 4
0
        public async Task UnlinkScopeAsync(string parentScopeName, string childScopeName)
        {
            var parentScope = await this.GetEntityAsync <Data.Scope>(s => s.Name == parentScopeName);

            var childScope = await this.GetEntityAsync <Data.Scope>(s => s.Name == childScopeName);

            var existingLink = await this.GetEntityAsync <Data.ScopeHierarchy>(s => s.ChildId == childScope.Id && s.ParentId == parentScope.Id);

            if (existingLink != null)
            {
                this.context.Set <Data.ScopeHierarchy>().Remove(existingLink);
                parentScope.ModificationBy   = principalIdProvider.PrincipalId;
                parentScope.ModificationDate = DateTime.UtcNow;
                childScope.ModificationBy    = principalIdProvider.PrincipalId;
                childScope.ModificationDate  = DateTime.UtcNow;

                (await SharedQueries.GetModelModificationDateAsync(this.context)).Scopes = DateTime.UtcNow;
            }
        }
Esempio n. 5
0
        private async Task DeleteScopeAsync(Data.Scope scope)
        {
            if (scope != null)
            {
                var childrenScopes = await this.context.Set <Data.Scope>()
                                     .Join(
                    this.context.Set <Data.ScopeHierarchy>(),
                    s => s.Name,
                    sh => sh.Parent.Name,
                    (s, sh) => new { Scope = s, ScopeHierarchy = sh })
                                     .Where(r => r.ScopeHierarchy.ParentId == scope.Id)
                                     .Select(r => r.ScopeHierarchy.Child)
                                     .ToListAsync();

                foreach (var childrenScope in childrenScopes)
                {
                    await DeleteScopeAsync(childrenScope);
                }

                this.context
                .ScopeHierarchies()
                .RemoveRange(await this.context.ScopeHierarchies()
                             .Where(sh => sh.ParentId == scope.Id)
                             .ToListAsync());

                this.context
                .ScopeHierarchies()
                .RemoveRange(await this.context.ScopeHierarchies()
                             .Where(sh => sh.ChildId == scope.Id)
                             .ToListAsync());

                this.context
                .Authorizations()
                .RemoveRange(await this.context.Authorizations()
                             .Where(a => a.ScopeId == scope.Id)
                             .ToListAsync());

                this.context.Scopes().Remove(scope);
                (await SharedQueries.GetModelModificationDateAsync(this.context)).Scopes = DateTime.UtcNow;
            }
        }
Esempio n. 6
0
        public async Task CreateScopeAsync(string scopeName, string description, params string[] parents)
        {
            var scope = await this.GetEntityAsync <Data.Scope>(s => s.Name == scopeName);

            if (scope == null)
            {
                scope = new Data.Scope
                {
                    Name           = scopeName,
                    Description    = description,
                    CreationBy     = this.principalIdProvider.PrincipalId,
                    ModificationBy = this.principalIdProvider.PrincipalId
                };

                this.context.Set <Data.Scope>().Add(scope);
                (await SharedQueries.GetModelModificationDateAsync(this.context)).Scopes = DateTime.UtcNow;
            }

            if (parents != null)
            {
                foreach (var parentName in parents)
                {
                    await this.CreateScopeAsync(parentName, parentName);

                    var parentScope = await this.GetEntityAsync <Data.Scope>(s => s.Name == parentName);

                    var scopeHierarchy = await this.GetEntityAsync <Data.ScopeHierarchy>(sh => sh.ChildId == scope.Id && sh.ParentId == parentScope.Id);

                    if (scopeHierarchy == null)
                    {
                        this.context.Set <Data.ScopeHierarchy>().Add(new Data.ScopeHierarchy
                        {
                            Child  = scope,
                            Parent = parentScope
                        });
                    }
                }
            }
        }
Esempio n. 7
0
        public async Task CreateRoleAsync(string roleName, string[] rightNames)
        {
            var role = await this.GetEntityAsync <Data.Role>(r => r.Name == roleName);

            if (role == null)
            {
                role = new Data.Role
                {
                    Name           = roleName,
                    CreationBy     = this.principalIdProvider.PrincipalId,
                    ModificationBy = this.principalIdProvider.PrincipalId
                };

                this.context.Set <Data.Role>().Add(role);
                (await SharedQueries.GetModelModificationDateAsync(this.context)).Roles = DateTime.UtcNow;
            }

            if (rightNames != null)
            {
                foreach (var rightName in rightNames)
                {
                    await this.CreateRightAsync(rightName);

                    var right = await GetEntityAsync <Data.Right>(r => r.Name == rightName);

                    if (right == null)
                    {
                        throw new InvalidOperationException($"Inconsistency with right : {rightName}. Specified right does not exist.");
                    }

                    role.Rights.Add(new Data.RoleRight
                    {
                        Right = right,
                        Role  = role
                    });
                    (await SharedQueries.GetModelModificationDateAsync(this.context)).Rights = DateTime.UtcNow;
                }
            }
        }