Esempio n. 1
0
        public async Task <APIResponseModel> Process(InternalSendEmailVerificationAction action)
        {
            var rsU = await this.DataManager.QueryUser(action.UserId);

            if (rsU.Code != ResponseCode.Ok)
            {
                return(APIResponseModel.Result(rsU.ToResponse()));
            }

            var user = rsU.Data[0].User;

            if (user == null)
            {
                return(APIResponseModel.Error(ResponseCode.NotFound));
            }
            if (user.EmailVerification == null)
            {
                return(APIResponseModel.Success());
            }

            string vs = await this.PasswordManager.BuildEmailVerificationString(user.UserId, user.EmailVerification.Code);

            var url = this.EmailProvider.BaseUrl() + "verify?code=" + Uri.EscapeDataString(vs);

            var cd = new Dictionary <string, string> {
                { "EmailVerificationUrl", url },
                { "DisplayName", user.DisplayName }
            };

            var rs = await this.EmailProvider.ProcessTemplate(action.UserId, EmailTemplateId.EmailVerification, user.Email, cd, user);

            return(APIResponseModel.Result(rs));
        }
Esempio n. 2
0
        public async Task <APIResponseModel> Process(UserSignupAction action)
        {
            // just be safe
            action.Email = action.Email.ToLower();

            if (string.IsNullOrWhiteSpace(action.Email))
            {
                return(APIResponseModel.Error(ResponseCode.InvalidParameter, "Email"));
            }
            if (string.IsNullOrWhiteSpace(action.Password))
            {
                return(APIResponseModel.Error(ResponseCode.InvalidParameter, "Password"));
            }
            if (action.Language == LanguageId.None)
            {
                action.Language = LanguageId.en;
            }

            var passwordStrength = new PasswordStrengthValidator().Test(action.Password);

            if (!passwordStrength.Good)
            {
                return(APIResponseModel.Error(ResponseCode.InvalidParameter, "Password Strength"));
            }

            // prepare the user
            UserPasswordModel password = new UserPasswordModel(await this.PasswordManager.CreatePasswordHash(action.Password));

            UserModel user = UserModel.Create(UserType.Standard, UserStatusId.Registered, action.Language, action.Email, password, action.DisplayName);

            var rs = await this.DataManager.CreateUserAsync(user);

            // failed - get out
            if (rs.Code != ResponseCode.Ok)
            {
                //await this.DataManager.LogEventAsync( LogEventModel.Failure( action.Action, user.ToJson(), user.UserId ) );
                return(APIResponseModel.Result(rs));
            }

            try
            {
                var j = new Newtonsoft.Json.Linq.JObject(
                    new Newtonsoft.Json.Linq.JProperty("userId", user.UserId.Encode()),
                    new Newtonsoft.Json.Linq.JProperty("email", user.Email ?? "{null}"),
                    new Newtonsoft.Json.Linq.JProperty("name", user.Name ?? "{null}")
                    ).ToString(Newtonsoft.Json.Formatting.Indented);
                this.DataManager.WriteEvent("user-created", j);                   // don't await - fire & forget
            }
            catch { }


            // move everything from the email address to the user
            try
            {
                rs = await this.DataManager.ConvertEmailToUserId(user);

                if (rs.Code != ResponseCode.Ok)
                {
                    await this.DataManager.LogErrorAsync("Process(UserSignupAction)", rs.ToJson());
                }
            }
            catch (Exception ex)
            {
                this.DataManager.LogExceptionAsync("Process(UserSignupAction)", ex);
            }

            // pipeline the email verification request
            try
            {
                APIResponseModel rs1 = await this.Process(new InternalSendEmailVerificationAction()
                {
                    UserId = user.UserId
                });
            }
            catch (Exception ex)
            {
                this.DataManager.LogErrorAsync("InternalSendEmailVerificationAction", ex.Message + Environment.NewLine + (ex.StackTrace ?? string.Empty));
            }

            this.SlackProvider.Send($"New user! {action.Email}");               //fire and forget

            ResponseData response = new ResponseData();

            response.Add(ResponseType.User, new UserViewModel(user));
            return(APIResponseModel.ResultWithData(response));
        }