Exemplo n.º 1
0
        public User AddTwitterUser(TwitterLite liteUser)
        {
            if (liteUser.Twitter == null || Extensions.ExtensionMethods.IsEmptyOrSpace(liteUser.Twitter.TwitterId))
                throw new ParamMissingException("Twitter ID cannot be empty.");
            else
                if (TwitterIdExists(liteUser.Twitter.TwitterId))
                    throw new AlreadyExistsException("Twitter ID already registered.");
            if (liteUser == null || liteUser.Credentials == null)
                throw new ParamMissingException("Missing credentials fields.");

            if (string.IsNullOrEmpty(liteUser.Credentials.Email))
                throw new InvalidValueException("Email cannot be empty.");

            //if (string.IsNullOrEmpty(liteUser.Credentials.Password))
            //    throw new InvalidValueException("Password cannot be empty.");

            if (Extensions.ExtensionMethods.IsValidEmail(liteUser.Credentials.Email) == false)
                throw new InvalidValueException("Invalid email provided.");
            if (UsernameExists(liteUser.Credentials.Username))
                throw new AlreadyExistsException("Username already exists.");

            if (EmailExists(liteUser.Credentials.Email))
                throw new AlreadyExistsException("Email already signed up.");

            Role role = Repository.Single<Role>(x => x.RoleId.Equals(liteUser.RoleId) && x.Deleted == false);
            if (role == null)
                throw new InvalidValueException("Role Id is invalid.");

            DateTime currentTime = DateTime.UtcNow;
            User user = new User
            {
                CreatedAt = currentTime,
                Credential = new Credential
                {
                    CreatedAt = currentTime,
                    CredentialId = Guid.NewGuid().ToString(),
                    Deleted = false,
                    Devices = new List<UserDevice>(0),
                    Email = liteUser.Credentials.Email,
                    LastModified = currentTime,
                    Password = liteUser.Credentials.Password, // Should already be hashed in calling function
                    WebSecurityToken = Guid.NewGuid().ToString(),
                    SecurityToken = Guid.NewGuid().ToString(),
                    TokenExpiry = DateTime.MaxValue,
                    Username = liteUser.Credentials.Username,
                },
                Deleted = false,
                Twitter = liteUser.Twitter,
                LastModified = currentTime,
                Address = liteUser.Address,
                Photo = liteUser.Photo,
                RoleId = liteUser.RoleId,
                UserId = Guid.NewGuid().ToString(),
                //Zipcode = liteUser.Zipcode,
                DisplayName = string.IsNullOrEmpty(liteUser.DisplayName) ?
                                        string.IsNullOrEmpty(liteUser.Twitter.TwitterId) ?
                                                liteUser.Credentials.Username : liteUser.Twitter.TwitterId
                                        : liteUser.DisplayName.Trim()
            };

            user.CredentialId = user.Credential.CredentialId;
            Repository.Add<User>(user);
            Repository.Save();
            return user;
        }
Exemplo n.º 2
0
        public HttpResponseMessage PostRegisterWithTwitter(TwitterLite user)
        {
            try
            {
                /*
                 * 1. Check if all required fields are present.
                 * 2. Take hash of Password.
                 * 3. Make gender lower case.
                 * 4. Check if username is already taken.
                 * 5. Check if email is already registered.
                 * 6. If not, create a new user and return its ID to user.
                 *
                 */
                if (user == null)
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Missing required param."));

                if (
                    //ExtensionMethods.IsEmptyOrSpace(user.Gender)
                    //|| ExtensionMethods.IsEmptyOrSpace(user.FirstName)
                    //|| ExtensionMethods.IsEmptyOrSpace(user.LastName)
                    ExtensionMethods.IsEmptyOrSpace(user.RoleId)
                    || user.Credentials == null
                    || ExtensionMethods.IsEmptyOrSpace(user.Credentials.Username)
                    //|| ExtensionMethods.IsEmptyOrSpace(user.Credentials.Password)
                    || user.Twitter == null
                    || ExtensionMethods.IsEmptyOrSpace(user.Twitter.TwitterAccessToken)
                    || ExtensionMethods.IsEmptyOrSpace(user.Twitter.TwitterId)
                    || ExtensionMethods.IsEmptyOrSpace(user.Twitter.Handle)
                    || ExtensionMethods.IsEmptyOrSpace(user.Twitter.TwitterSecret)
                    || (ExtensionMethods.IsEmptyOrSpace(user.Twitter.Email)
                        && ExtensionMethods.IsEmptyOrSpace(user.Credentials.Email))
                    //|| ExtensionMethods.IsEmptyOrSpace(user.Credentials.MobileNumber)
                    )
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Missing required param."));

                //user.Credentials.Password = ExtensionMethods.ToSHA1EncodedString(user.Credentials.Password);
                user.Credentials.Username = user.Credentials.Username.Trim().ToLower();

                if (string.IsNullOrEmpty(user.Twitter.Email) == false)
                    user.Twitter.Email = user.Twitter.Email.ToLower();

                if (user.Credentials.Email == null || ExtensionMethods.IsEmptyOrSpace(user.Credentials.Email))
                    user.Credentials.Email = user.Twitter.Email;
                // Check if already signed up.
                if (_userManager.EmailExists(user.Credentials.Email))
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Email already registered."));

                //user.Credentials.Username = user.Twitter.TwitterId;
                User savedUser = _userManager.AddTwitterUser(user);
                UserLite lite = new UserLite();
                lite.CopyFromUserIncludeCredentials(savedUser);

                UserCreationResponse result = new UserCreationResponse
                {
                    UserId = savedUser.UserId,
                    Token = savedUser.Credential.SecurityToken,
                    User = lite
                };
                return Request.CreateResponse(HttpStatusCode.OK, result);
            }
            catch (ParamMissingException e){
                return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse{ Message = e.Message });
            }catch (AlreadyExistsException e){
                return Request.CreateResponse(HttpStatusCode.Conflict, new ErrorResponse{ Message = e.Message });
            }catch (InvalidValueException e){
                return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse{ Message = e.Message });
            }catch (UserNotFoundException e){
                return Request.CreateResponse(HttpStatusCode.NotFound, new ErrorResponse{ Message = e.Message });
            }catch (Exception e){
                return Request.CreateResponse(HttpStatusCode.InternalServerError, new ErrorResponse { Message = "Oops, server encountered an issue... " + e.Message });
            }
        }