Beispiel #1
0
        /// <summary>
        /// Logic to update UserAuth from Registration info, not enabled on OnPut because of security.
        /// </summary>
        public object UpdateUserAuth(Registration request)
        {
            if (EndpointHost.RequestFilters == null ||
                !EndpointHost.RequestFilters.Contains(ValidationFilters.RequestFilter))
            {
                RegistrationValidator.ValidateAndThrow(request, ApplyTo.Put);
            }

            if (ValidateFn != null)
            {
                var response = ValidateFn(this, HttpMethods.Put, request);
                if (response != null)
                {
                    return(response);
                }
            }

            var session      = this.GetSession();
            var existingUser = UserAuthRepo.GetUserAuth(session, null);

            if (existingUser == null)
            {
                throw HttpError.NotFound("User does not exist");
            }

            var newUserAuth = ToUserAuth(request);

            UserAuthRepo.UpdateUserAuth(newUserAuth, existingUser, request.Password);

            return(new RegistrationResponse {
                UserId = existingUser.Id.ToString(CultureInfo.InvariantCulture),
            });
        }
        /// <summary>
        /// Create new Registration
        /// </summary>
        public override object OnPost(Registration request)
        {
            if (!ValidationFeature.Enabled)
            {
                RegistrationValidator.ValidateAndThrow(request, ApplyTo.Post);
            }

            AssertUserAuthRepo();

            if (ValidateFn != null)
            {
                var response = ValidateFn(this, HttpMethods.Post, request);
                if (response != null)
                {
                    return(response);
                }
            }

            var session      = this.GetSession();
            var newUserAuth  = ToUserAuth(request);
            var existingUser = UserAuthRepo.GetUserAuth(session, null);

            var user = existingUser != null
                                ? this.UserAuthRepo.UpdateUserAuth(existingUser, newUserAuth, request.Password)
                                : this.UserAuthRepo.CreateUserAuth(newUserAuth, request.Password);

            if (request.AutoLogin.GetValueOrDefault())
            {
                var authService = base.ResolveService <AuthService>();
                var response    = authService.Post(new Auth {
                    UserName = request.UserName ?? request.Email,
                    Password = request.Password
                });

                if (response is IHttpError)
                {
                    throw (Exception)response;
                }

                var authResponse = response as AuthResponse;
                if (authResponse != null)
                {
                    return(new RegistrationResponse {
                        SessionId = authResponse.SessionId,
                        UserName = authResponse.UserName,
                        ReferrerUrl = authResponse.ReferrerUrl,
                        UserId = user.Id.ToString(CultureInfo.InvariantCulture),
                    });
                }
            }

            return(new RegistrationResponse {
                UserId = user.Id.ToString(CultureInfo.InvariantCulture),
            });
        }
Beispiel #3
0
        /// <summary>
        /// Create new Registration
        /// </summary>
        public object Post(Registration request)
        {
            if (EndpointHost.RequestFilters == null ||
                !EndpointHost.RequestFilters.Contains(ValidationFilters.RequestFilter))    //Already gets run
            {
                RegistrationValidator.ValidateAndThrow(request, ApplyTo.Post);
            }

            AssertUserAuthRepo();

            if (ValidateFn != null)
            {
                var validateResponse = ValidateFn(this, HttpMethods.Post, request);
                if (validateResponse != null)
                {
                    return(validateResponse);
                }
            }

            RegistrationResponse response = null;
            var session      = this.GetSession();
            var newUserAuth  = ToUserAuth(request);
            var existingUser = UserAuthRepo.GetUserAuth(session, null);

            var registerNewUser = existingUser == null;
            var user            = registerNewUser
                ? this.UserAuthRepo.CreateUserAuth(newUserAuth, request.Password)
                : this.UserAuthRepo.UpdateUserAuth(existingUser, newUserAuth, request.Password);

            if (registerNewUser)
            {
                session.OnRegistered(this);
            }

            if (request.AutoLogin.GetValueOrDefault())
            {
                using (var authService = base.ResolveService <AuthService>())
                {
                    var authResponse = authService.Post(new Auth {
                        UserName = request.UserName ?? request.Email,
                        Password = request.Password,
                        Continue = request.Continue
                    });

                    if (authResponse is IHttpError)
                    {
                        throw (Exception)authResponse;
                    }

                    var typedResponse = authResponse as AuthResponse;
                    if (typedResponse != null)
                    {
                        response = new RegistrationResponse {
                            SessionId   = typedResponse.SessionId,
                            UserName    = typedResponse.UserName,
                            ReferrerUrl = typedResponse.ReferrerUrl,
                            UserId      = user.Id.ToString(CultureInfo.InvariantCulture),
                        };
                    }
                }
            }

            if (response == null)
            {
                response = new RegistrationResponse {
                    UserId      = user.Id.ToString(CultureInfo.InvariantCulture),
                    ReferrerUrl = request.Continue
                };
            }

            var isHtml = base.RequestContext.ResponseContentType.MatchesContentType(ContentType.Html);

            if (isHtml)
            {
                if (string.IsNullOrEmpty(request.Continue))
                {
                    return(response);
                }

                return(new HttpResult(response)
                {
                    Location = request.Continue
                });
            }

            return(response);
        }