예제 #1
0
        public object ValidateAndCreateUser(RegisterModel model)
        {
            var  message  = string.Empty;
            User authUser = null;

            //_userRegistrationValidators.Validate(model);

            if (!string.IsNullOrEmpty(model.AuthId))
            {
                var oAuthIdExist = false;
                var emailIdExist = false;
                _accessTokenRepository.CheckOAuthUserExistsAndCheckEmailExists(model.AuthId, model.UserName, out oAuthIdExist, out emailIdExist);
                if (emailIdExist)
                {
                    var user = _accessTokenRepository.GetUserByEmail(model.UserName);
                    if (user != null)
                    {
                        model.Id = user.Id;
                        var loginMap = LoginWithFacebookMapperForOAuthUserEmail(model);
                        _accessTokenRepository.OAuthEmailUserUpdate(loginMap);
                        authUser = user;
                        var emailToken = TokenMapper(authUser as User, _accessTokenRepository.CreateToken(authUser));
                        return(emailToken);
                    }
                }
                else
                {
                    var user = UserAudienceCredentialsMapper(model);
                    user.IsVerified = true;
                    user.UserCode   = Convert.ToString(Guid.NewGuid());
                    SaveUserCode(user.UserCode);
                    var userId = _accessTokenRepository.CreateNewUser(user);
                    user.Id  = userId;
                    authUser = user;
                    message  = "Användare tillagd.";
                    var emailToken = TokenMapper(authUser as User, _accessTokenRepository.CreateToken(authUser), message);
                    return(emailToken);
                }
            }
            else
            {
                _userRegistrationValidators.Validate(model);
                var user = UserAudienceCredentialsMapper(model);
                user.UserCode = Convert.ToString(Guid.NewGuid());
                SaveUserCode(user.UserCode);
                var userId = _accessTokenRepository.CreateNewUser(user);
                user.Id  = userId;
                authUser = user;
                message  = "Användare tillagd.";
            }
            var webUrl = ConfigurationManager.AppSettings["WebUrl"];

            var fields = new StringDictionary
            {
                { "signUpUrl", string.Format("{0}{1}{2}{3}", Convert.ToString(webUrl), "/Email/ConfirmEmail?id=", _cryptoGraphy.EncryptString(Convert.ToString(authUser.Id)), "&email=" + _cryptoGraphy.EncryptString(authUser.Email)) }
            };

            message = "Vi har sänt dig ett verifikationsemail, vänligen verifiera din emailadress.";

            var htmlBody = _fm.ReadFileContents(GetMailerTemplatePath("html", "CreateUser")).ReplaceMatch(fields);

            _emailNotifier.SendEmail(authUser.Email, htmlBody, "Verify Link");
            var registerResponse = new RegisterResponse
            {
                UserId  = authUser.Id,
                Message = message
            };

            //var token = TokenMapper(authUser as User, _accessTokenRepository.CreateToken(authUser));
            //token.Message = message;
            return(registerResponse);
        }