public Task <IdentityManagerResult <CreateResult> > CreateRoleAsync(IEnumerable <PropertyValue> properties)
        {
            var errors = ValidateRoleProperties(properties);

            if (errors.Any())
            {
                return(Task.FromResult(new IdentityManagerResult <CreateResult>(errors.ToArray())));
            }

            var role            = new InMemoryRole();
            var createPropsMeta = GetMetadata().RoleMetadata.GetCreateProperties();

            foreach (var prop in properties)
            {
                var result = SetRoleProperty(createPropsMeta, role, prop.Type, prop.Value);
                if (!result.IsSuccess)
                {
                    return(Task.FromResult(new IdentityManagerResult <CreateResult>(result.Errors.ToArray())));
                }
            }

            if (roles.Any(x => x.Name.Equals(role.Name, StringComparison.OrdinalIgnoreCase)))
            {
                return(Task.FromResult(new IdentityManagerResult <CreateResult>("Role name already in use.")));
            }

            roles.Add(role);

            return(Task.FromResult(new IdentityManagerResult <CreateResult>(new CreateResult()
            {
                Subject = role.ID
            })));
        }
        public static ICollection<InMemoryRole> Get(int random = 0)
        {
            var roles = new HashSet<InMemoryRole>
            {
                new InMemoryRole{
                    ID = Guid.Parse("991d965f-1f84-4360-90e4-8f6deac7b9bc").ToString(),
                    Name = "Admin",
                    Description = "They run the show"
                },
                new InMemoryRole{
                    ID = Guid.Parse("5f292677-d3d2-4bf9-a6f8-e982d08e1377").ToString(),
                    Name = "Manager",
                    Description = "They pay the bills"
                },
            };

            for (var i = 0; i < random; i++)
            {
                var user = new InMemoryRole
                {
                    Name = GenName()
                };
                roles.Add(user);
            }

            return roles;
        }
        public static ICollection <InMemoryRole> Get(int random = 0)
        {
            var roles = new HashSet <InMemoryRole>
            {
                new InMemoryRole {
                    ID          = Guid.Parse("991d965f-1f84-4360-90e4-8f6deac7b9bc").ToString(),
                    Name        = "Admin",
                    Description = "They run the show"
                },
                new InMemoryRole {
                    ID          = Guid.Parse("5f292677-d3d2-4bf9-a6f8-e982d08e1377").ToString(),
                    Name        = "Manager",
                    Description = "They pay the bills"
                },
            };

            for (var i = 0; i < random; i++)
            {
                var user = new InMemoryRole
                {
                    Name = GenName()
                };
                roles.Add(user);
            }

            return(roles);
        }
Пример #4
0
        private void SetRoleProperty(IEnumerable <PropertyMetadata> propsMeta, InMemoryRole role, string type, string value)
        {
            if (propsMeta.TrySet(role, type, value))
            {
                return;
            }

            throw new InvalidOperationException("Invalid Property Type : " + type);
        }
        private string GetRoleProperty(PropertyMetadata property, InMemoryRole role)
        {
            string value;

            if (property.TryGet(role, out value))
            {
                return(value);
            }

            throw new Exception("Invalid property type " + property.Type);
        }
        private void SetRoleProperty(IEnumerable<PropertyMetadata> propsMeta, InMemoryRole role, string type, string value)
        {
            if (propsMeta.TrySet(role, type, value))
            {
                return;
            }

            throw new InvalidOperationException("Invalid Property Type : " + type);
        }
        private string GetRoleProperty(PropertyMetadata property, InMemoryRole role)
        {
            string value;
            if (property.TryGet(role, out value))
            {
                return value;
            }

            throw new Exception("Invalid property type " + property.Type);
        }
        public Task<IdentityManagerResult<CreateResult>> CreateRoleAsync(IEnumerable<Property> properties)
        {
            var errors = ValidateRoleProperties(properties);
            if (errors.Any())
            {
                return Task.FromResult(new IdentityManagerResult<CreateResult>(errors.ToArray()));
            }

            var role = new InMemoryRole();
            var createPropsMeta = GetMetadata().RoleMetadata.GetCreateProperties();
            foreach (var prop in properties)
            {
                SetRoleProperty(createPropsMeta, role, prop.Type, prop.Value);
            }

            roles.Add(role);

            return Task.FromResult(new IdentityManagerResult<CreateResult>(new CreateResult() { Subject = role.ID }));
        }
        private IdentityManagerResult SetRoleProperty(IEnumerable <PropertyMetadata> propsMeta, InMemoryRole role, string type, string value)
        {
            IdentityManagerResult result;

            if (propsMeta.TrySet(role, type, value, out result))
            {
                return(result);
            }

            throw new InvalidOperationException("Invalid Property Type : " + type);
        }
        public Task<IdentityManagerResult<CreateResult>> CreateRoleAsync(IEnumerable<PropertyValue> properties)
        {
            var errors = ValidateRoleProperties(properties);
            if (errors.Any())
            {
                return Task.FromResult(new IdentityManagerResult<CreateResult>(errors.ToArray()));
            }

            var role = new InMemoryRole();
            var createPropsMeta = GetMetadata().RoleMetadata.GetCreateProperties();
            foreach (var prop in properties)
            {
                SetRoleProperty(createPropsMeta, role, prop.Type, prop.Value);
            }

            if (roles.Any(x=>x.Name.Equals(role.Name, StringComparison.OrdinalIgnoreCase)))
            {
                return Task.FromResult(new IdentityManagerResult<CreateResult>("Role name already in use."));
            }

            roles.Add(role);

            return Task.FromResult(new IdentityManagerResult<CreateResult>(new CreateResult() { Subject = role.ID }));
        }