Ejemplo n.º 1
0
        public async Task <APIResponseModel> Process(APIRequestModel request, SampleUser user = null)
        {
            switch (request.Action)
            {
            case ActionType.Status: throw new NotImplementedException();
            //case ActionType.AdminDeleteUser: return await this.Process( user, request.AdminDeleteUser() );
            //case ActionType.AdminReload: return await this.Process( user, request.AdminReload() );

            //case ActionType.InternalSendEmailVerification: return await this.Process( request.InternalSendEmailVerification() );

            case ActionType.UserCancel: return(await this.Process(user, request.UserCancel()));

            case ActionType.UserSignup: return(await this.Process(request.UserSignup()));

            case ActionType.UserSiteInfo: return(await this.Process(user, request.UserSiteInfo()));

            case ActionType.UserUpdateDisplayName: return(await this.Process(user, request.UserUpdateDisplayName()));

            case ActionType.UserUpdatePassword: return(await this.Process(user, request.UserUpdatePassword()));

            case ActionType.UserVerifyEmail: return(await this.Process(request.UserVerifyEmail()));

            default: return(APIResponseModel.Error(ResponseCode.Internal_UnhandledError, "ActionType not implemented in Action Processor"));
            }
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public async Task <APIResponseModel> Process(SampleUser userContext, UserSiteInfoAction action)
        {
            UserModel user = null;

            if (userContext.SiteUser != null)
            {
                user = userContext.OBO != null ? userContext.OBO : userContext.SiteUser;
            }

            if (user.SiteSettings == null)
            {
                user.SiteSettings = new UserSiteInfoModel();
            }
            foreach (var kvp in action.Settings)
            {
                if (user.SiteSettings.Settings.ContainsKey(kvp.Key))
                {
                    user.SiteSettings.Settings[kvp.Key] = kvp.Value;
                }
                else
                {
                    user.SiteSettings.Settings.Add(kvp.Key, kvp.Value);
                }
            }
            var dte = UserEntity.UpdateSiteBuilder(user.UserId, user.SiteSettings);
            var rs  = await this.DataManager.UpdateEntityProperty(EntityTableType.user, dte);

            if (rs.Code != ResponseCode.Ok)
            {
                return(APIResponseModel.Error(rs.Code, rs.Message));
            }
            return(APIResponseModel.ResultWithData(ResponseType.SiteInfo, new UserSiteInfoViewModel(user.SiteSettings)));
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 5
0
        public async Task <APIResponseModel> Process(SampleUser userContext, UserUpdatePasswordAction action)
        {
            UserModel user = userContext.OBO != null ? userContext.OBO : userContext.SiteUser;

            if (string.IsNullOrWhiteSpace(action.NewPassword))
            {
                return(APIResponseModel.Error(ResponseCode.InvalidParameter, "Password"));
            }

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

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

            if (!(await this.PasswordManager.Verify(action.OldPassword, user.Password.Hash)))
            {
                return(APIResponseModel.Error(ResponseCode.InvalidCredentials));
            }
            else
            {
                var pwd = new UserPasswordModel(await this.PasswordManager.CreatePasswordHash(action.NewPassword));
                var rs  = await this.DataManager.UpdateEntityProperty(EntityTableType.user, UserEntity.UpdatePasswordBuilder(user.UserId, pwd, PasswordMode.UpdatePassword));

                if (rs.Code != ResponseCode.Ok)
                {
                    return(APIResponseModel.Error(rs.Code, rs.Message));
                }
            }

            this.DataManager.WriteEvent("user-password-updated", action);               // don't await - fire & forget

            return(APIResponseModel.Success());
        }
Ejemplo n.º 6
0
        public async Task OBO(string messageId, string oboEmail)
        {
            APIResponseModel response = null;
            string           token    = string.Empty;

            UserModel oboUser = null;
            var       rsO     = await this.DataManager.QueryUser(oboEmail);

            if (rsO.Code == ResponseCode.Ok)
            {
                oboUser = rsO.Data[0].User;
                if (!this.DataManager.Loaded(DataManagerDataType.System))
                {
                    response = APIResponseModel.Error(ResponseCode.Loading);
                }
                else
                {
                    response = await Check(oboUser);
                }

                token = this.TokenManager.BuildToken((response.Data[ResponseType.User] as UserViewModel).UserId, oboUser);
            }
            else
            {
                response = APIResponseModel.Error(ResponseCode.NotFound, "User not found");
            }

            await Clients.Caller.SendAsync("obo", messageId, response.ToJson(), token);
        }
Ejemplo n.º 7
0
        public async Task <APIResponseModel> Process(SampleUser userContext, UserCancelAction action)
        {
            UserModel user = userContext.OBO != null ? userContext.OBO : userContext.SiteUser;

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

            return(APIResponseModel.Error(rs.Code, rs.Message));
        }
Ejemplo n.º 8
0
        public async Task VerifyEmail(string messageId, string code)
        {
            APIResponseModel response = null;

            if (!Crypto.TryParseEmailVerificationString(code, out Guid userId, out string verificationCode))
            {
                response = APIResponseModel.Error(ResponseCode.EmailVerificationCodeExpired, "The verification email has expired. Please request another one before continuing");
            }
Ejemplo n.º 9
0
        public async Task Config()
        {
            //do something
            var response = !this.DataManager.Loaded(DataManagerDataType.System)
                                ? APIResponseModel.Error(ResponseCode.Loading)
                                : APIResponseModel.ResultWithData(ResponseType.Metadata, new MetadataViewModel(this.DataManager.Metadata));

            await Clients.Caller.SendAsync("config", response.ToJson());
        }
Ejemplo n.º 10
0
        public async Task <APIResponseModel> Process(SampleUser userContext, UserUpdateDisplayNameAction action)
        {
            UserModel user = userContext.OBO != null ? userContext.OBO : userContext.SiteUser;

            var dte = UserEntity.UpdateDisplayName(user.UserId, action.DisplayName);
            var rs  = await this.DataManager.UpdateEntityProperty(EntityTableType.user, dte);

            if (rs.Code != ResponseCode.Ok)
            {
                return(APIResponseModel.Error(rs.Code, rs.Message));
            }

            return(APIResponseModel.Success());
        }
Ejemplo n.º 11
0
        public async Task RevertOBO(string messageId)
        {
            UserModel user  = this.SampleUser != null ? this.SampleUser.SiteUser : null;
            var       token = this.TokenManager.BuildToken(user.UserId);

            APIResponseModel response = null;

            if (!this.DataManager.Loaded(DataManagerDataType.System))
            {
                response = APIResponseModel.Error(ResponseCode.Loading);
            }
            else
            {
                response = await Check(user);
            }

            await Clients.Caller.SendAsync("robo", messageId, response.ToJson(), token);
        }
Ejemplo n.º 12
0
        public async Task <APIResponseModel> Process(UserVerifyEmailAction action)
        {
            var rs = await this.DataManager.VerifyContactAsync(action.UserId, ContactMethodType.Email, action.Code);

            switch (rs.Code)
            {
            case ResponseCode.Ok:

                this.DataManager.WriteEvent("user-email-verified", action);                           // don't await - fire & forget
                break;

            default:

                // don't wait, just queue
                this.DataManager.WriteEvent(action.Action.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(rs));
                break;
            }

            return(APIResponseModel.Error(rs.Code, rs.Message));
        }
Ejemplo n.º 13
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));
        }