Exemple #1
0
 public UserViewModel(User user)
 {
     this.Id = user.Id;
     this.Email = user.Email;
     this.Username = user.UserName;
     this.FullName = user.FullName;
     this.GravatarImageUrl = UserService.GetGravatarImageUrl(user.GravatarId);
     this.Role = user.Role;
     this.Url = ConfigService.AppPath + "user/" + user.Id;
 }
Exemple #2
0
 public static bool TryAuthorizeUser(User user, UserRole role)
 {
     return user.IsInRole(role);
 }
Exemple #3
0
        public static bool TryAuthenticateUser(Guid userGuid, out User user)
        {
            using (var db = DataService.Connect(true))
            {
                user = db.FirstOrDefault<User>(u => u.Guid == userGuid);
            }

            return user != null;
        }
Exemple #4
0
        public static bool TryAuthenticateUser(string username, string password, out User user)
        {
            username = String.IsNullOrEmpty(username) ? String.Empty : username.ToLowerInvariant();

            if (!QueryService.TryGetUser(username, out user))
            {
                return false;
            }

            string hash = UserService.CalculatePasswordHash(user.Guid, user.Salt, password);
            return user.PasswordHash.Equals(hash, StringComparison.Ordinal);
        }
Exemple #5
0
        public PopulateResults PopulateWithData(NameValueCollection data, User user, bool checkRequired = false)
        {
            PopulateResults results = new PopulateResults();
            string username = null;

            foreach (string name in data.AllKeys)
            {
                string[] values = data.GetValues(name);
                string value = values[values.Length - 1].Trim();
                switch (name.ToLowerInvariant())
                {
                    case "email":
                        {
                            string email = value.ToLowerInvariant();
                            if (email != this.Email)
                            {
                                string gravatar = UserService.GenerateGravatarId(email);
                                string verifyToken = UserService.GenerateVerifyToken();

                                results.Updates.Add("Email", new PopulateResults.UpdatedValue()
                                {
                                    Old = this.Email,
                                    New = this.Email = email,
                                });

                                results.Updates.Add("GravatarId", new PopulateResults.UpdatedValue()
                                {
                                    Old = this.GravatarId,
                                    New = this.GravatarId = gravatar,
                                });

                                results.Updates.Add("VerifyToken", new PopulateResults.UpdatedValue()
                                {
                                    Old = this.VerifyToken,
                                    New = this.VerifyToken = verifyToken,
                                });
                            }
                        }
                        break;

                    case "fullname":
                        if (value != this.FullName)
                        {
                            results.Updates.Add("FullName", new PopulateResults.UpdatedValue()
                            {
                                Old = this.FullName,
                                New = this.FullName = value,
                            });
                        }
                        break;

                    case "username":
                        if (String.IsNullOrEmpty(value))
                        {
                            username = String.Empty;
                        }
                        else if (UsernameValidation.IsMatch(value))
                        {
                            username = value;
                        }
                        else
                        {
                            results.Errors.Add(new ValidationError() { Field = "username", Message = "Usernames must be three to fifteen characters long and can only contain letters and numbers." });
                        }
                        break;

                    case "role":
                        if (user.IsInRole(UserRole.Admin))
                        {
                            UserRole role;
                            if (Enum.TryParse(value, true, out role))
                            {
                                if (role != this.Role)
                                {
                                    results.Updates.Add("Role", new PopulateResults.UpdatedValue()
                                    {
                                        Old = this.Role,
                                        New = this.Role = role,
                                    });
                                }
                            }
                            else
                            {
                                results.Errors.Add(new ValidationError() { Field = name, Message = "Unknown user role." });
                            }
                        }
                        else
                        {
                            results.Errors.Add(new ValidationError() { Field = name, Message = "Forbidden." });
                        }
                        break;
                }
            }

            // Check username last since it might default to an updated email.
            if (username != null)
            {
                if (String.IsNullOrEmpty(username))
                {
                    username = this.Email;
                }

                else if (username != this.UserName)
                {
                    results.Updates.Add("UserName", new PopulateResults.UpdatedValue()
                    {
                        Old = this.UserName,
                        New = this.UserName = username,
                    });
                }
            }

            if (checkRequired)
            {
                if (String.IsNullOrEmpty(this.Email))
                {
                    results.Errors.Add(new ValidationError() { Field = "email", Message = "Required." });
                }
            }

            return results;
        }
Exemple #6
0
        public static bool TryGetUserByName(Guid currentUserGuid, string name, out User user)
        {
            using (var db = DataService.Connect(true))
            {
                user = (currentUserGuid != Guid.Empty && "[me]".Equals(name, StringComparison.OrdinalIgnoreCase)) ?
                        db.FirstOrDefault<User>(u => u.Guid == currentUserGuid) :
                        db.FirstOrDefault<User>(u => u.UserName == name);
            }

            return user != null;
        }
Exemple #7
0
        public static bool TryGetUser(Guid currentUserGuid, string nameOrEmail, out User user)
        {
            nameOrEmail = String.IsNullOrEmpty(nameOrEmail) ? String.Empty : nameOrEmail.ToLowerInvariant();

            using (var db = DataService.Connect(true))
            {
                user = (currentUserGuid != Guid.Empty && "[me]".Equals(nameOrEmail, StringComparison.OrdinalIgnoreCase)) ?
                        db.FirstOrDefault<User>(u => u.Guid == currentUserGuid) :
                        db.FirstOrDefault<User>(u => u.UserName == nameOrEmail || u.Email == nameOrEmail);
            }

            return user != null;
        }
Exemple #8
0
 public static bool TryGetUser(string nameOrEmail, out User user)
 {
     return TryGetUser(Guid.Empty, nameOrEmail, out user);
 }