Beispiel #1
0
        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
            })));
        }
Beispiel #2
0
        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"
                },
                new InMemoryRole {
                    ID          = Guid.NewGuid().ToString(),
                    Name        = "Employee",
                    Description = "They perform the work"
                }
            };

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

            return(roles);
        }
Beispiel #3
0
 private async Task <string> GetRoleProperty(PropertyMetadata property, InMemoryRole role)
 {
     if (property.TryGet(role, out var value))
     {
         return(await value);
     }
     throw new Exception("Invalid property type " + property.Type);
 }
Beispiel #4
0
 private IdentityManagerResult SetRoleProperty(IEnumerable <PropertyMetadata> propsMeta, InMemoryRole role, string type, string value)
 {
     if (propsMeta.TrySet(role, type, value, out var result))
     {
         return(result);
     }
     throw new InvalidOperationException("Invalid Property Type : " + type);
 }