Exemplo n.º 1
0
        public BaseResponse ResetPasswordRequest(DynamicDictionary _parameters)
        {
            BaseResponse response = new BaseResponse();

            string json = Request.Body.ReadAsString();

            if (json != null)
            {
                ResetRequest rr = JsonConvert.DeserializeObject <ResetRequest>(json);
                if (rr.Email != null &&
                    rr.InstallId != null)
                {
                    HydrantWikiManager manager = new HydrantWikiManager();
                    User user = manager.GetUserByEmail(UserSources.HydrantWiki, rr.Email);
                    if (user != null)
                    {
                        //Send reset password email
                        PasswordReset reset = PasswordReset.GetNewRequest(user.Guid);
                        manager.Persist(reset);

                        NameValueCollection nvc = new NameValueCollection
                        {
                            { "Code", reset.Code }
                        };

                        manager.SendCannedEmail(user, CannedEmailNames.ResetPasswordEmail, nvc);
                        manager.LogInfo(
                            user.Guid,
                            string.Format(
                                "Password Reset Request for user {0} on {1}",
                                user.Username,
                                rr.InstallId));

                        response.Success = true;
                        response.Message = "Password reset email sent.";
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "";
                    }
                }
            }

            return(response);
        }
Exemplo n.º 2
0
        private BaseResponse EmailInUse(DynamicDictionary _parameters)
        {
            HydrantWikiManager  hwm      = new HydrantWikiManager();
            IsAvailableResponse response = new IsAvailableResponse {
                Available = false, Success = true
            };

            string email = _parameters["email"];

            if (email != null)
            {
                User user = hwm.GetUserByEmail(UserSources.HydrantWiki, email);

                TraceFileHelper.Info("Check if email in use ({0})", email);

                if (user == null)
                {
                    response.Available = true;
                }
            }

            return(response);
        }
Exemplo n.º 3
0
        private BaseResponse IsAvailable(DynamicDictionary _parameters)
        {
            HydrantWikiManager  hwm      = new HydrantWikiManager();
            IsAvailableResponse response = new IsAvailableResponse {
                Available = false, Success = true
            };

            string username = _parameters["username"];

            if (username != null)
            {
                User user = hwm.GetUser(UserSources.HydrantWiki, username);

                TraceFileHelper.Info("Check if username exists ({0})", username);

                if (user == null)
                {
                    response.Available = true;
                }
            }

            return(response);
        }
Exemplo n.º 4
0
        public BaseResponse CreateAccount(DynamicDictionary _parameters)
        {
            BaseResponse       response = new BaseResponse();
            HydrantWikiManager hwm      = new HydrantWikiManager();

            try
            {
                string json = Request.Body.ReadAsString();
                Objects.CreateAccount account = JsonConvert.DeserializeObject <Objects.CreateAccount>(json);

                User user = hwm.GetUser(UserSources.HydrantWiki, account.Username);
                if (user == null)
                {
                    user = hwm.GetUserByEmail(UserSources.HydrantWiki, account.Email);
                    if (user == null)
                    {
                        user              = new User();
                        user.Guid         = Guid.NewGuid();
                        user.Active       = true;
                        user.DisplayName  = account.Username;
                        user.Username     = account.Username;
                        user.EmailAddress = account.Email;
                        user.UserSource   = UserSources.HydrantWiki;
                        user.UserType     = UserTypes.User;
                        user.IsVerified   = false;
                        hwm.Persist(user);

                        TGUserPassword userPassword = TGUserPassword.GetNew(user.Guid, user.Username, account.Password);
                        hwm.Persist(userPassword);

                        TGUserEmailValidation validation = new TGUserEmailValidation(user);
                        hwm.Persist(validation);

                        NameValueCollection nvc = new NameValueCollection
                        {
                            { "SystemUrl", Config.GetSettingValue("SystemUrl") },
                            { "ValidationText", validation.ValidationText }
                        };
                        hwm.SendCannedEmail(user, CannedEmailNames.ValidateEmailAddress, nvc);
                        hwm.LogInfo(user.Guid, "User created");

                        response.Success = true;
                        response.Message = "Please check your email to finish activating your account";
                        return(response);
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "Email already in use.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Username already exists.";
                }

                hwm.LogWarning(Guid.Empty, response.Message);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = "An error occurred";
                response.Error   = "An error occurred";
                hwm.LogException(Guid.Empty, ex);
            }

            return(response);
        }
Exemplo n.º 5
0
        public Response ValidateEmail(DynamicDictionary _parameters)
        {
            const string success = @"
                <html>
                    <head>
                    </head>
                    <body>
                        <p>You have successfully validated your email with HydrantWiki.</p>
                        <p><a href=""www.hydrantwiki.com"">HydrantWiki</a></p>
                    </body>
                </html>";

            const string failure = @"
                <html>
                    <head>
                    </head>
                    <body>
                        <p>Unable to validate your email with HydrantWiki.</p>
                        <p><a href=""www.hydrantwiki.com"">HydrantWiki</a></p>
                    </body>
                </html>";

            string validationToken = _parameters["token"];

            HydrantWikiManager hwManager = new HydrantWikiManager();

            if (!string.IsNullOrEmpty(validationToken))
            {
                TGUserEmailValidation uev = hwManager.GetTGUserEmailValidation(validationToken);

                if (uev != null &&
                    uev.ParentGuid != null)
                {
                    User user = (User)hwManager.GetUser(uev.ParentGuid.Value);

                    if (user != null)
                    {
                        user.IsVerified = true;

                        hwManager.Persist(user);
                        hwManager.Delete(uev);
                        hwManager.LogInfo(user.Guid, string.Format("Validated email address ({0})", user.EmailAddress));

                        Response successResponse = Response.AsText(success);
                        successResponse.ContentType = "text/html";
                        return(successResponse);
                    }
                    else
                    {
                        //User not found.
                        hwManager.LogWarning(Guid.Empty, string.Format("User not found (Token:{0})", validationToken));
                    }
                }
                else
                {
                    //Validation text not found in database
                    hwManager.LogWarning(Guid.Empty, string.Format("Validated token not found ({0})", validationToken));
                }
            }
            else
            {
                //Validation text not supplied.
                hwManager.LogWarning(Guid.Empty, "Validation token not supplied");
            }

            Response failureResponse = Response.AsText(failure);

            failureResponse.ContentType = "text/html";
            return(failureResponse);
        }
Exemplo n.º 6
0
        public BaseResponse ResetPassword(DynamicDictionary _parameters)
        {
            BaseResponse response = new BaseResponse();

            string json = Request.Body.ReadAsString();

            if (json != null)
            {
                ResetPassword rr = JsonConvert.DeserializeObject <ResetPassword>(json);
                if (rr.Email != null &&
                    rr.InstallId != null &&
                    rr.Code != null &&
                    rr.NewPassword != null)
                {
                    HydrantWikiManager manager = new HydrantWikiManager();
                    User user = manager.GetUserByEmail(UserSources.HydrantWiki, rr.Email);
                    if (user != null)
                    {
                        DateTime      now = DateTime.UtcNow;
                        PasswordReset pr  = manager.GetPasswordReset(user.Guid, rr.Code);

                        if (pr != null)
                        {
                            if (pr.CreationDateTime > now.AddHours(-2))
                            {
                                TGUserPassword userPassword = TGUserPassword.GetNew(
                                    user.Guid,
                                    user.Username,
                                    rr.NewPassword);
                                manager.Persist(userPassword);

                                pr.Active = false;
                                manager.Persist(pr);

                                manager.LogInfo(user.Guid, "Password successfully reset");

                                response.Success = true;
                                response.Message = "Password successfully reset.";
                            }
                            else
                            {
                                manager.LogWarning(user.Guid, "Password Reset request has expired");
                                response.Success = false;
                                response.Message = "Password Reset request has expired";
                            }
                        }
                        else
                        {
                            manager.LogWarning(user.Guid, "Invalid reset code");
                            response.Success = false;
                            response.Message = "Invalid reset code.";
                        }
                    }
                    else
                    {
                        TraceFileHelper.Warning("User not found ({0})", rr.Email);
                        response.Success = false;
                        response.Message = "User not found.";
                    }
                }
                else
                {
                    response.Message = "Invalid information supplied";
                }
            }
            else
            {
                response.Message = "Body not supplied";
            }

            return(response);
        }