public object Save(Entities.User User) { if (!ModelState.IsValid) { return(Error(ModelState)); } if (User.DateOfBirth.HasValue) { User.DateOfBirth = User.DateOfBirth.Value.AddMinutes(UTCOffset); } if (User.Id > 0) { var u = Entities.User.Find(Id: User.Id); User.Password = u.Password; User.Salt = u.Salt; } if (User.Save()) { return(Success(new { Entity = User.AdminJson() })); } return(Error()); }
public object Register(UserModel User) { if (!ModelState.IsValid) { return(Error(ModelState)); } var user = new Entities.User { FirstName = User.FirstName, LastName = User.LastName, Telephone = User.Telephone, EmailAddress = User.EmailAddress, DateOfBirth = User.DateOfBirth, GenderId = User.GenderId, ClearPassword = User.PIN, LanguageId = User.LanguageId ?? Entities.Language.English }; if (!Regex.IsMatch(user.Telephone, @"^07[\d]{8}")) { return(Error($"{user.Telephone} is not a valid cell number. The number should start with 07 and contain 10 digits.")); } if (!user.UniqueTelephone()) { return(Error($"{user.Telephone} has already been registered")); } if (!user.EmailAddress.IsEmpty() && !user.UniqueEmailAddress()) { return(Error($"{user.EmailAddress} has already been registered")); } user.RoleList = $"{Entities.Role.User}"; user.NotificationPreferences = (int)Entities.NotificationPreferences.PushNotifications + (int)Entities.NotificationPreferences.SMS; user.StatusId = Entities.UserStatus.Pending; if (!Entities.User.VerificationRequired) { user.StatusId = UserStatus.Verified; if (user.AuthToken.IsEmpty()) { user.AuthToken = Guid.NewGuid().ToString(); } } if (user.Save()) { Entities.Counter.Hit(CurrentUser.Id, Entities.Counters.Register); if (Entities.User.VerificationRequired) { if (!user.SendVerificationCode()) { user.Delete(); return(Error("Unable to send verification code - please try again")); } } return(Success(new { User = user.ProfileJson() })); } return(Error("An unknown error occurred.")); }