Exemplo n.º 1
0
        public async Task Signup(string messageId, UserSignupAction action)
        {
            APIResponseModel response = null;
            string           token    = null;

            try
            {
                var msg = this.DataManager.Metadata.GetTextForResponseCode(ResponseCode.Loading, action.Language);

                if (!this.DataManager.Loaded(DataManagerDataType.System))
                {
                    response = APIResponseModel.Error(ResponseCode.Loading, msg);
                }
                else
                {
                    response = await this.ActionProcessor.Process(action);

                    if (response.Code == ResponseCode.Ok)
                    {
                        token = this.TokenManager.BuildToken((response.Data[ResponseType.User] as UserViewModel).UserId);
                    }
                }

                msg = this.DataManager.Metadata.GetTextForResponseCode(response.Code, action.Language);
                response.SetMessage(msg);
            }
            catch (Exception ex)
            {
                response = APIResponseModel.Exception(ex);
            }

            await Clients.Caller.SendAsync("signup", messageId, response.ToJson(), token);
        }
Exemplo 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));
        }