Ejemplo n.º 1
0
        public Task <IdentityManagerResult <CreateResult> > CreateUserAsync(IEnumerable <PropertyValue> properties)
        {
            var errors = ValidateUserProperties(properties);

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

            var user            = new InMemoryUser();
            var createPropsMeta = GetMetadata().UserMetadata.GetCreateProperties();

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

            if (users.Any(x => x.Username.Equals(user.Username, StringComparison.OrdinalIgnoreCase)))
            {
                return(Task.FromResult(new IdentityManagerResult <CreateResult>("Username already in use.")));
            }

            users.Add(user);

            return(Task.FromResult(new IdentityManagerResult <CreateResult>(new CreateResult()
            {
                Subject = user.Subject
            })));
        }
Ejemplo n.º 2
0
        private string GetName(InMemoryUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException();
            }

            return(user.Claims.GetValue(Constants.ClaimTypes.Name));
        }
Ejemplo n.º 3
0
        public static ICollection <InMemoryUser> Get(int random = 0)
        {
            var users = new HashSet <InMemoryUser>
            {
                new InMemoryUser {
                    Subject  = Guid.Parse("081d965f-1f84-4360-90e4-8f6deac7b9bc").ToString(),
                    Username = "******",
                    Password = "******",
                    Email    = "*****@*****.**",
                    Mobile   = "123",
                    Claims   = new HashSet <Claim> {
                        new Claim(Constants.ClaimTypes.Name, "Alice Smith"),
                        new Claim(Constants.ClaimTypes.Role, "admin"),
                        new Claim(Constants.ClaimTypes.Role, "employee"),
                        new Claim(Constants.ClaimTypes.Role, "manager"),
                        new Claim("department", "sales"),
                    }
                },
                new InMemoryUser {
                    Subject  = Guid.Parse("5f292677-d3d2-4bf9-a6f8-e982d08e1306").ToString(),
                    Username = "******",
                    Password = "******",
                    Email    = "*****@*****.**",
                    Claims   = new HashSet <Claim> {
                        new Claim(Constants.ClaimTypes.Name, "Bob Smith"),
                        new Claim(Constants.ClaimTypes.Role, "employee"),
                        new Claim(Constants.ClaimTypes.Role, "developer"),
                        new Claim("department", "IT"),
                    }
                },
                new InMemoryUser {
                    Subject  = Guid.NewGuid().ToString(),
                    Username = "******",
                    Password = "******",
                    Email    = "*****@*****.**",
                    Claims   = new HashSet <Claim> {
                        new Claim(Constants.ClaimTypes.Name, "Test"),
                        new Claim(Constants.ClaimTypes.Role, "employee"),
                        new Claim(Constants.ClaimTypes.Role, "developer"),
                        new Claim("department", "IT"),
                    }
                },
            };

            for (var i = 0; i < random; i++)
            {
                var user = new InMemoryUser
                {
                    Username = GenName().ToLower()
                };
                user.Claims.Add(new Claim("name", GenName() + " " + GenName()));
                users.Add(user);
            }

            return(users);
        }
Ejemplo n.º 4
0
        private IdentityManagerResult SetName(InMemoryUser user, string value)
        {
            if (user == null)
            {
                throw new ArgumentNullException("SetName::" + string.Format(ExceptionMessages.IsNotAssigned, user));
            }
            if (value == null)
            {
                throw new ArgumentNullException("SetName::" + string.Format(ExceptionMessages.IsNotAssigned, value));
            }

            user.Claims.SetValue(Constants.ClaimTypes.Name, value);
            return(IdentityManagerResult.Success);
        }
Ejemplo n.º 5
0
        private async Task <string> GetUserProperty(PropertyMetadata property, InMemoryUser user)
        {
            if (property.TryGet(user, out var value))
            {
                return(await value);
            }

            switch (property.Type)
            {
            case "role.admin":
                return(user.Claims.HasValue(Constants.ClaimTypes.Role, "admin").ToString().ToLower());

            case "gravatar":
                return(user.Claims.GetValue("gravatar"));
            }

            throw new Exception("Invalid property type " + property.Type);
        }
Ejemplo n.º 6
0
        private IdentityManagerResult SetUserProperty(IEnumerable <PropertyMetadata> propsMeta, InMemoryUser user, string type, string value)
        {
            if (propsMeta.TrySet(user, type, value, out var result))
            {
                return(result);
            }

            switch (type)
            {
            case "role.admin":
            {
                var val = bool.Parse(value);
                if (val)
                {
                    user.Claims.AddClaim(Constants.ClaimTypes.Role, "admin");
                }
                else
                {
                    user.Claims.RemoveClaim(Constants.ClaimTypes.Role, "admin");
                }
            }
            break;

            case "gravatar":
            {
                user.Claims.SetValue("gravatar", value);
            }
            break;

            default:
                throw new InvalidOperationException("Invalid Property Type : " + type);
            }

            return(IdentityManagerResult.Success);
        }
Ejemplo n.º 7
0
 public Task <string> GetUserPropertyValue(PropertyMetadata property, InMemoryUser user)
 {
     return(GetUserProperty(property, user));
 }