Example #1
0
        public ActionResult AddNewConcierge(FormCollection collection)
        {
            string name = collection.Get("newConciergeName");
            string email = collection.Get("newConciergeEmail");
            string password = collection.Get("newConciergePassword");

            if (name == null)
                throw new Reop.Managers.Exceptions.ParamMissingException("Name is required");
            if (email == null)
                throw new Reop.Managers.Exceptions.ParamMissingException("Email is required");
            if (password == null)
                throw new Reop.Managers.Exceptions.ParamMissingException("Password is required");

            UserLite newUser = new UserLite();

            newUser.Credentials = new CredentialsLite();
            newUser.Credentials.Email = email.Trim().ToLower();
            newUser.Credentials.Password = Extensions.ExtensionMethods.ToSHA1EncodedString(password.Trim());
            newUser.DisplayName = name.Trim();
            newUser.Credentials.Username = email.Trim().ToLower();
            newUser.RoleId = "2";
            User registeredUser = null;
            try
            {
                registeredUser = _userManager.AddUser(newUser);
            }
            catch (Exception exp)
            {
                Trace.WriteLine("[ERROR] " + exp.StackTrace);
            }

            if (registeredUser != null)
            {
                try
                {
                    // Agent has been registered, lets also create it on zendesk.
                    long? zendeskId = _services.Ticketing.RegisterAgent(registeredUser, registeredUser.Credential.Email);
                    bool registeredWithZendesk = _userManager.UpdateUserZendeskData(registeredUser.UserId, zendeskId, null);
                    Trace.WriteLine(string.Format("Creation of ZENDESK AGENT user for {0} return {1}"
                        , registeredUser.Credential.Email, registeredWithZendesk));
                }
                catch (Exception e)
                {
                    Trace.WriteLine("[ERROR] Failed to register user with Zendesk. EMAIL=" + registeredUser.Credential.Email);
                    Trace.TraceError(e.Message);
                    Trace.TraceError(e.StackTrace);
                }
            }

            //return JsonConvert.SerializeObject(new WebCookieLite(registeredUser));

            return View("concierge");
        }
Example #2
0
        public bool UpdateUser(UserLite liteUser)
        {
            if (liteUser == null || liteUser.UserId == null || liteUser.UserId.Trim().Length == 0)
                throw new ParamMissingException("Missing required user info for update.");

            User user = Repository.Single<User>(liteUser.UserId);
            if (user == null)
                return false;

            if(!string.IsNullOrEmpty(liteUser.DisplayName))
                user.DisplayName = liteUser.DisplayName;
            user.Photo = liteUser.Photo;
            user.Address = liteUser.Address;
            user.LastModified = DateTime.UtcNow;
            Repository.Save();
            return true;
        }
Example #3
0
        public UserLite GetUserDetails(string userId)
        {
            User user = Repository.Single<User>(x => x.UserId.Equals(userId) && x.Deleted != true);
            if (user == null)
                throw new UserNotFoundException("User not found.");
            UserLite lite = new UserLite();
            lite.CopyFromUser(user);

            return lite;
        }
Example #4
0
        public List<UserLite> GetAllUsersLite(int startCount = 0, int takeCount = 50)
        {
            List<UserLite> results = new List<UserLite>();
            var query = Repository.Query<User>(u => u.Deleted == false).
                OrderByDescending(u => u.CreatedAt).
                Skip(startCount).
                Take(takeCount);

            if (query.Any())
            {
                // There are results to send back, but we should not send complete objects.
                foreach (User user in query.AsEnumerable<User>())
                {
                    UserLite liteUser = new UserLite();
                    liteUser.CopyFromUser(user);
                    results.Add(liteUser);
                }
            }
            return results;
        }
Example #5
0
        public User AddUser(UserLite liteUser)
        {
            if (liteUser == null || liteUser.Credentials == null)
                throw new ParamMissingException("Missing member fields.");

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

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

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

            if (UsernameExists(liteUser.Credentials.Username))
                throw new AlreadyExistsException("Username already exists.");

            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,
                Facebook = null,
                LastModified = currentTime,
                Photo = liteUser.Photo,
                Address = liteUser.Address,
                RoleId = liteUser.RoleId,
                UserId = Guid.NewGuid().ToString(),
                DisplayName = string.IsNullOrEmpty(liteUser.DisplayName) ?
                                        liteUser.Credentials.Username : liteUser.DisplayName.Trim()
            };

            user.CredentialId = user.Credential.CredentialId;
            Repository.Add<User>(user);
            Repository.Save();
            return user;
        }
Example #6
0
        public string Register(string email, string password, string fullName)
        {
            UserLite newUser = new UserLite();

            newUser.Credentials = new CredentialsLite();
            newUser.Credentials.Email = email.Trim().ToLower();
            newUser.Credentials.Password = Extensions.ExtensionMethods.ToSHA1EncodedString(password.Trim());
            newUser.DisplayName = fullName.Trim();
            newUser.Credentials.Username = email.Trim().ToLower();
            newUser.RoleId = "1";
            User registeredUser=null;
            try
            {
                registeredUser = _userManager.AddUser(newUser);
            }
            catch (BaseException exp)
            {
                //Console.WriteLine(exp.StackTrace);
                //return //new System.Web.Mvc.HttpNotFoundResult(exp.Message);// HttpException(409, exp.Message);
                return JsonConvert.SerializeObject(new RegisterUserResponse
                {
                    Message = exp.Message,
                    Status = 400,
                    WebCookie = null
                });
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception while registering user.");
                Console.WriteLine(e.StackTrace);
                return JsonConvert.SerializeObject(new RegisterUserResponse
                {
                    Message = "We could not process your request at this time. [" + e.Message + "]",
                    Status = 500,
                    WebCookie = null
                });
            }
            return JsonConvert.SerializeObject(new RegisterUserResponse{ Message = "OK", Status = 200, WebCookie =
                new WebCookieLite(registeredUser, Request.UrlReferrer)});
        }
Example #7
0
 public ActionResult LoginWithFacebook(FbCredentials userCred)
 {
     try
     {
         if (userCred == null
             || ExtensionMethods.IsEmptyOrSpace(userCred.FbId)
             || ExtensionMethods.IsEmptyOrSpace(userCred.FbAccessToken))
             //  return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Missing required param"));
             return null;
         User user = _userManager.LoginWithFacebook(userCred);
         if (user == null)
            // return Request.CreateResponse(HttpStatusCode.Unauthorized, GetErrorResponse("Login failed."));
             return null;
         else
         {
             UserLite lite = new UserLite();
             lite.CopyFromUserIncludeCredentials(user);
             UserCreationResponse result = new UserCreationResponse
             {
                 UserId = user.UserId,
                 Token = user.Credential.SecurityToken,
                 User = lite
             };
            // return Request.CreateResponse(HttpStatusCode.OK, result);
             return null;
         }
     }
     catch (ParamMissingException e)
     {
         return null;
        // return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse(e.Message));
     }
     catch (AlreadyExistsException e)
     {
         return null;
        // return Request.CreateResponse(HttpStatusCode.Conflict, GetErrorResponse(e.Message));
     }
     catch (InvalidValueException e)
     {
         return null;
        // return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse(e.Message));
     }
     catch (UserNotFoundException e)
     {
         return null;
         //return Request.CreateResponse(HttpStatusCode.NotFound, GetErrorResponse(e.Message));
     }
     catch (Exception e)
     {
         return null;
         //return Request.CreateResponse(HttpStatusCode.InternalServerError, GetErrorResponse("Oops, server encountered an issue... " + e.Message));
     }
 }
Example #8
0
 public HttpResponseMessage VerifyUser()
 {
     try
     {
         /*
          * This method is called when user launches their app.
          * It should give back user info, including pending requests and all.
          * Right now lets just reutrn the User as other things are not implemneted yet.
          * BUT REMEMBER TO UPDATE THIS AS FUNCTIONALITY GROWS.
          *
          */
         UserLite lite = new UserLite();
         // credentials are loaded in basic authentication along with user.
         // just use that object.
         lite.CopyFromUserIncludeCredentials(this.User.User);
         return Request.CreateResponse(HttpStatusCode.OK, lite);
     }
     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 });
     }
 }
Example #9
0
        public HttpResponseMessage Put(UserLite user)
        {
            try
            {
                if (string.IsNullOrEmpty(user.UserId))
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Please set UserId."));

                if (!IsValidUser(user.UserId))
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Not authorized to update this user."));

                // User is same, just pass this object down for update.
                if (_userManager.UpdateUser(user))
                    return Request.CreateResponse(HttpStatusCode.OK);
                else
                    return Request.CreateResponse(HttpStatusCode.NotModified, GetErrorResponse("User not found or could not be updated right now."));
            }
            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 });
            }
        }
Example #10
0
        public HttpResponseMessage PostRegisterWithUsername(UserLite 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.FirstName)
                    //|| ExtensionMethods.IsEmptyOrSpace(user.LastName)
                    //||
                    ExtensionMethods.IsEmptyOrSpace(user.RoleId)
                    || user.Credentials == null
                    || ExtensionMethods.IsEmptyOrSpace(user.Credentials.Email)
                    || ExtensionMethods.IsEmptyOrSpace(user.Credentials.Username)
                    || ExtensionMethods.IsEmptyOrSpace(user.Credentials.Password)
                    //|| 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 (user.Credentials.Email.IsValidEmail() == false)
                    return Request.CreateResponse(HttpStatusCode.BadRequest, GetErrorResponse("Incorrect email address provided."));
                // Check if already signed up.
                if (_userManager.EmailExists(user.Credentials.Email))
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Email already registered."));

                if (_userManager.UsernameExists(user.Credentials.Username))
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Username already registered."));

                User savedUser = _userManager.AddUser(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});
            }
        }
Example #11
0
        public HttpResponseMessage Login(CredentialsLite userCred)
        {
            try
            {
                if (userCred == null
                    //|| ExtensionMethods.IsEmptyOrSpace(userCred.Email)
                    || (ExtensionMethods.IsEmptyOrSpace(userCred.Username)
                    && ExtensionMethods.IsEmptyOrSpace(userCred.Email))
                    || ExtensionMethods.IsEmptyOrSpace(userCred.Password))
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Missing required param"));

                userCred.Password = ExtensionMethods.ToSHA1EncodedString(userCred.Password);
                //userCred.Email = userCred.Email.Trim().ToLower();
                if(string.IsNullOrWhiteSpace(userCred.Username) == false)
                    userCred.Username = userCred.Username.Trim().ToLower();
                if (string.IsNullOrWhiteSpace(userCred.Email) == false)
                    userCred.Email = userCred.Email.Trim().ToLower();
                User user = _userManager.Login(userCred);
                if (user == null)
                    return Request.CreateResponse(HttpStatusCode.Unauthorized, GetErrorResponse("Login failed."));
                else
                {
                    UserLite lite = new UserLite();
                    lite.CopyFromUserIncludeCredentials(user);
                    UserCreationResponse result = new UserCreationResponse
                    {
                        UserId = user.UserId,
                        Token = user.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 });
            }
        }