/// <summary>
        /// Registers the specified user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public Status<User> RegisterUser(User user, string password)
        {
            user.CreateDateUtc = DateTime.UtcNow;
            user.UpdateDateUtc = DateTime.UtcNow;
            user.UpdatedBy = "AccountAdapter";

            // validate the object
            var result = Status.Validatate<User>(user);
            if (result.StatusCode != 200)
                return result;

            if (string.IsNullOrWhiteSpace(password))
                return Status.ValidationError<User>(null, "Password", "The password was not specified");

            using (var context = new RentlerContext())
            {
                try
                {
                    // check for dup email
                    var dupEmail = context.Users.Where(u => !u.IsDeleted && u.Email.ToLower() == user.Email.ToLower()).Count();
                    if (dupEmail > 0)
                    {
                        return Status.ValidationError<User>(null, "Email", "The email is already used");
                    }

                    // check for dup username
                    var dupUser = context.Users.Where(u => !u.IsDeleted && u.Username.ToLower() == user.Username.ToLower()).Count();
                    if (dupUser > 0)
                    {
                        return Status.ValidationError<User>(null, "Username", "The username is already used");
                    }

                    user.PasswordHash = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");

                    // create the user
                    context.Users.Add(user);
                    context.SaveChanges();

                    // notify user by email that their password was changed successfully.
                    EmailAccountRegisterModel model = new EmailAccountRegisterModel()
                    {
                        Name = string.Format("{0} {1}", user.FirstName, user.LastName),
                        To = user.Email
                    };
                    mailer.Register(model);

                    return Status<User>.OK(user);
                }
                catch (Exception ex)
                {
                    // TODO: log exception
                    return Status.Error<User>("System was unable to create user", null);
                }
            }
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the Users EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToUsers(User user)
 {
     base.AddObject("Users", user);
 }
Example #3
0
        public Status<Guid> GetAuthToken(ApiKey apiKey, string affiliateUserKey,
            string email, string passwordHash,
            string firstName, string lastName, string username)
        {
            RedisPublisher.Publish("token","Getting token for api key: " + apiKey.ToString());

            //check if user exists
            var userId = accountAdapter.GetAffiliateUserIdByUsernameOrEmailAndApiKey(email, apiKey.ApiKeyId);

            //did we not find anything?
            if (userId.StatusCode == 404)
            {
                //try again, looking for regular rentler users this time
                userId = accountAdapter.GetUserIdByUsernameOrEmail(email);

                //still didn't find anything?
                if (userId.StatusCode == 404)
                {
                    //we've got a new one, make 'em
                    var user = new User();
                    user.Email = email;
                    user.FirstName = firstName;
                    user.LastName = lastName ?? string.Empty;
                    user.Username = username;
                    user.PasswordHash = string.Empty;
                    user.CreateDateUtc = DateTime.UtcNow;
                    user.UpdatedBy = "ksl auth";
                    user.UpdateDateUtc = DateTime.UtcNow;

                    var status = Status.Validatate<User>(user);

                    if (status.StatusCode != 200)
                    {
                        var result = Status.NotFound<Guid>();
                        result.Errors = status.Errors;

                        return result;
                    }

                    using (var context = new RentlerContext())
                    {
                        context.Users.Add(user);
                        context.SaveChanges();
                        userId = Status.OK(user.UserId);
                    }
                }

                //Okay, now they're either already a Rentler user but are coming in from an affiliate,
                //or they just became one from an affiliate, so create an affiliate user for them.
                using (var context = new RentlerContext())
                {
                    var affiliate = new AffiliateUser();
                    affiliate.UserId = userId.Result;
                    affiliate.AffiliateUserKey = affiliateUserKey;
                    affiliate.ApiKey = apiKey.ApiKeyId;
                    affiliate.PasswordHash = passwordHash;
                    context.AffiliateUsers.Add(affiliate);
                    context.SaveChanges();
                }
            }

            //generate auth token
            var token = CreateAuthToken(userId.Result);

            RedisPublisher.Publish("token", "Returning token: " + token.ToString());

            //return token
            return Status.OK(token);
        }
 /// <summary>
 /// Create a new User object.
 /// </summary>
 /// <param name="userId">Initial value of the UserId property.</param>
 /// <param name="username">Initial value of the Username property.</param>
 /// <param name="email">Initial value of the Email property.</param>
 /// <param name="updateDateUtc">Initial value of the UpdateDateUtc property.</param>
 /// <param name="createDateUtc">Initial value of the CreateDateUtc property.</param>
 /// <param name="updatedBy">Initial value of the UpdatedBy property.</param>
 /// <param name="isDeleted">Initial value of the IsDeleted property.</param>
 /// <param name="referenceId">Initial value of the ReferenceId property.</param>
 public static User CreateUser(global::System.Int32 userId, global::System.String username, global::System.String email, global::System.DateTime updateDateUtc, global::System.DateTime createDateUtc, global::System.String updatedBy, global::System.Boolean isDeleted, global::System.Guid referenceId)
 {
     User user = new User();
     user.UserId = userId;
     user.Username = username;
     user.Email = email;
     user.UpdateDateUtc = updateDateUtc;
     user.CreateDateUtc = createDateUtc;
     user.UpdatedBy = updatedBy;
     user.IsDeleted = isDeleted;
     user.ReferenceId = referenceId;
     return user;
 }
        public static IEnumerable<Rentler.Data.User> ConvertToUsers(IEnumerable<Member> members)
        {
            Console.WriteLine("Converting Ksl members to Rentler users...");
            var users = new List<User>();

            foreach(var item in members)
            {
                var u = new User()
                {
                    UserId = Guid.NewGuid(),
                    CreateDate = DateTime.UtcNow,
                    CreatedBy = "ksl import",
                    Email = item.email,
                    FirstName = item.first,
                    LastName = item.last,
                    PhoneNumber = item.priPhone,
                    Username = item.email
                };

                u.AffiliateUser = new AffiliateUser()
                {
                    AffiliateUserKey = item.id.ToString(),
                    ApiKey = new Guid("DA05C498-C338-4EE4-9A70-6872E38349CC"),
                    PasswordHash = item.passwordHash
                };

                users.Add(u);
            }

            return users;
        }