示例#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"));
            }
        }
示例#2
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)));
        }
示例#3
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());
        }
示例#4
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));
        }
示例#5
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());
        }
示例#6
0
        public async Task Signin(UserModel user, HttpContext context)
        {
            var claims = new List <Claim>();

            claims.Add(new Claim(ClaimTypes.NameIdentifier, user.UserId.Encode()));
            var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

            var authProperties = new AuthenticationProperties
            {
                IsPersistent = true,
                IssuedUtc    = DateTimeOffset.UtcNow,
                AllowRefresh = true
            };

            SampleUser p = new SampleUser(user, claimsIdentity);

            await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, p, authProperties);
        }
示例#7
0
        public bool TryPrepareUserFrom(HttpContext httpContext, out SampleUser tfUser)
        {
            tfUser = null;

            var claim = httpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);

            if (claim == null)
            {
                return(false);
            }

            try
            {
                Guid userId = Guid.Parse(claim.Value);
                var  obo    = httpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.UserData);

                UserModel user = null, oboUser = null;

                var rsC = this.DataManager.QueryUser(userId, IncludeOptions.None).GetAwaiter().GetResult();
                if (rsC.Code != ResponseCode.Ok)
                {
                    return(false);
                }
                user = rsC.Data[0].User;

                if (obo != null)
                {
                    rsC = this.DataManager.QueryUser(obo.Value.Decode(), IncludeOptions.None).GetAwaiter().GetResult();
                    if (rsC.Code != ResponseCode.Ok)
                    {
                        return(false);
                    }
                    oboUser = rsC.Data[0].User;
                }

                tfUser           = new SampleUser(user, httpContext.User.Identity, oboUser);
                httpContext.User = tfUser;
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
示例#8
0
 public void InjectUserId(SampleUser u) => this.UserId = u.OBO != null ? u.OBO.UserId : u.SiteUser.UserId;