Ejemplo n.º 1
0
        /// <summary> Activate account </summary>
        public DataResult <ActivationRequest> ActivateAccount(string activationToken)
        {
            DataResult <ActivationRequest> drDefaultResponse = new DataResult <ActivationRequest>()
            {
                Success = false, ErrorMessage = "Unable to process your activation request"
            };
            DataResult <ActivationRequest> drActivationRequest = database.GetActivationRequest(activationToken);

            if (!drActivationRequest.Success)
            {
                return(drDefaultResponse);
            }

            ActivationRequest activationRequest = drActivationRequest.Data.FirstOrDefault();

            if (activationRequest == null)
            {
                return(drDefaultResponse);
            }

            DataResult <User> drUser = database.GetUser(new User {
                Id = activationRequest.UserId
            });

            if (!drUser.Success)
            {
                return(drDefaultResponse);
            }

            User user = drUser.Data.FirstOrDefault();

            if (user == null)
            {
                return(drDefaultResponse);
            }

            user.Activated = true;

            DataResult <User> drUserUpdated = database.UpdateUser(user);

            if (!drUserUpdated.Success)
            {
                return(drDefaultResponse);
            }

            DataResult <ActivationRequest> drDeleteRecoveryRequest = database.DeleteActivationRequest(activationRequest);

            return(drDeleteRecoveryRequest);
        }
Ejemplo n.º 2
0
        /// <summary> Register a new user </summary>
        public DataResult <User> RegisterUser(User user)
        {
            Validator validator = new Validator();

            user.CreationDate = DateTime.Now;

            string hashedPassword = validator.PasswordHasher(user);

            user.Password = hashedPassword;

            DataResult <User> dr = database.CreateUser(user);

            if (!dr.Success)
            {
                return(dr);
            }

            // Todo => send an email to the user before the account is activated
            Mailer mailer = new Mailer();

            string activationToken = validator.RandomTemporaryString(12);

            ActivationRequest activationRequest = new ActivationRequest
            {
                UserId = user.Id,
                Token  = activationToken
            };

            database.StoreActivationRequest(activationRequest);

            string link = DASHBOARD_URL + "activate/" + activationToken;

            string body = string.Empty;

            body += "Hi, \n\n";
            body += "<a href=" + link + ">Click here</a> to activate your account \n\n";
            body += "Monoid Inc.";

            string title     = "Account activation";
            string subject   = "Monoid Dashboard: " + title;
            string recipient = user.EmailAddress;

            mailer.SendEmail(body, subject, title, new[] { recipient });

            return(dr);
        }
Ejemplo n.º 3
0
        // Delete activationrequest
        public DataResult <ActivationRequest> DeleteActivationRequest(ActivationRequest ar)
        {
            DataResult <ActivationRequest> result = new DataResult <ActivationRequest>();

            if (!mOnline)
            {
                result.Success      = false;
                result.ErrorMessage = DB_ERROR;
                return(result);
            }

            try
            {
                GetActivationRequestCollection().DeleteOne(Builders <ActivationRequest> .Filter.Eq(x => x.Token, ar.Token));
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success      = false;
                result.ErrorMessage = ex.Message;
            }

            return(result);
        }
Ejemplo n.º 4
0
        // Store activationrequest
        public DataResult <ActivationRequest> StoreActivationRequest(ActivationRequest ar)
        {
            DataResult <ActivationRequest> result = new DataResult <ActivationRequest>();

            if (!mOnline)
            {
                result.Success      = false;
                result.ErrorMessage = DB_ERROR;
                return(result);
            }

            try
            {
                GetActivationRequestCollection().InsertOne(ar);
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success      = false;
                result.ErrorMessage = ex.Message;
            }

            return(result);
        }