DocSpace-buildtools/web/ASC.Web.Api/Api/AuthenticationController.cs

593 lines
23 KiB
C#
Raw Normal View History

2022-02-28 15:31:03 +00:00
using AuthenticationException = System.Security.Authentication.AuthenticationException;
using Constants = ASC.Core.Users.Constants;
using SecurityContext = ASC.Core.SecurityContext;
2022-03-01 10:58:02 +00:00
namespace ASC.Web.Api.Controllers;
[Scope]
[DefaultRoute]
[ApiController]
[AllowAnonymous]
public class AuthenticationController : ControllerBase
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
private readonly UserManager _userManager;
private readonly TenantManager _tenantManager;
private readonly SecurityContext _securityContext;
private readonly TenantCookieSettingsHelper _tenantCookieSettingsHelper;
private readonly CookiesManager _cookiesManager;
private readonly PasswordHasher _passwordHasher;
private readonly EmailValidationKeyModelHelper _emailValidationKeyModelHelper;
private readonly ICache _cache;
private readonly SetupInfo _setupInfo;
private readonly MessageService _messageService;
private readonly ProviderManager _providerManager;
private readonly IOptionsSnapshot<AccountLinker> _accountLinker;
private readonly CoreBaseSettings _coreBaseSettings;
private readonly PersonalSettingsHelper _personalSettingsHelper;
private readonly StudioNotifyService _studioNotifyService;
private readonly UserHelpTourHelper _userHelpTourHelper;
private readonly Signature _signature;
private readonly InstanceCrypto _instanceCrypto;
private readonly DisplayUserSettingsHelper _displayUserSettingsHelper;
private readonly MessageTarget _messageTarget;
private readonly StudioSmsNotificationSettingsHelper _studioSmsNotificationSettingsHelper;
private readonly SettingsManager _settingsManager;
private readonly SmsManager _smsManager;
private readonly TfaManager _tfaManager;
private readonly TimeZoneConverter _timeZoneConverter;
private readonly SmsKeyStorage _smsKeyStorage;
private readonly CommonLinkUtility _commonLinkUtility;
private readonly ApiContext _apiContext;
private readonly AuthContext _authContext;
private readonly UserManagerWrapper _userManagerWrapper;
public AuthenticationController(
UserManager userManager,
TenantManager tenantManager,
SecurityContext securityContext,
TenantCookieSettingsHelper tenantCookieSettingsHelper,
CookiesManager cookiesManager,
PasswordHasher passwordHasher,
EmailValidationKeyModelHelper emailValidationKeyModelHelper,
ICache cache,
SetupInfo setupInfo,
MessageService messageService,
ProviderManager providerManager,
IOptionsSnapshot<AccountLinker> accountLinker,
CoreBaseSettings coreBaseSettings,
PersonalSettingsHelper personalSettingsHelper,
StudioNotifyService studioNotifyService,
UserManagerWrapper userManagerWrapper,
UserHelpTourHelper userHelpTourHelper,
Signature signature,
InstanceCrypto instanceCrypto,
DisplayUserSettingsHelper displayUserSettingsHelper,
MessageTarget messageTarget,
StudioSmsNotificationSettingsHelper studioSmsNotificationSettingsHelper,
SettingsManager settingsManager,
SmsManager smsManager,
TfaManager tfaManager,
TimeZoneConverter timeZoneConverter,
SmsKeyStorage smsKeyStorage,
CommonLinkUtility commonLinkUtility,
ApiContext apiContext,
AuthContext authContext)
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
_userManager = userManager;
_tenantManager = tenantManager;
_securityContext = securityContext;
_tenantCookieSettingsHelper = tenantCookieSettingsHelper;
_cookiesManager = cookiesManager;
_passwordHasher = passwordHasher;
_emailValidationKeyModelHelper = emailValidationKeyModelHelper;
_cache = cache;
_setupInfo = setupInfo;
_messageService = messageService;
_providerManager = providerManager;
_accountLinker = accountLinker;
_coreBaseSettings = coreBaseSettings;
_personalSettingsHelper = personalSettingsHelper;
_studioNotifyService = studioNotifyService;
_userHelpTourHelper = userHelpTourHelper;
_signature = signature;
_instanceCrypto = instanceCrypto;
_displayUserSettingsHelper = displayUserSettingsHelper;
_messageTarget = messageTarget;
_studioSmsNotificationSettingsHelper = studioSmsNotificationSettingsHelper;
_settingsManager = settingsManager;
_smsManager = smsManager;
_tfaManager = tfaManager;
_timeZoneConverter = timeZoneConverter;
_smsKeyStorage = smsKeyStorage;
_commonLinkUtility = commonLinkUtility;
_apiContext = apiContext;
_authContext = authContext;
_userManagerWrapper = userManagerWrapper;
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
[Read]
public bool GetIsAuthentificated()
{
return _securityContext.IsAuthenticated;
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
[Create("{code}", false, order: int.MaxValue)]
public AuthenticationTokenResponseDto AuthenticateMeFromBodyWithCode([FromBody] AuthDto auth)
{
return AuthenticateMeWithCode(auth);
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
[Create("{code}", false, order: int.MaxValue)]
[Consumes("application/x-www-form-urlencoded")]
public AuthenticationTokenResponseDto AuthenticateMeFromFormWithCode([FromForm] AuthDto auth)
{
return AuthenticateMeWithCode(auth);
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
[Create(false)]
public Task<AuthenticationTokenResponseDto> AuthenticateMeFromBodyAsync([FromBody] AuthDto auth)
{
return AuthenticateMeAsync(auth);
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
[Create(false)]
[Consumes("application/x-www-form-urlencoded")]
public Task<AuthenticationTokenResponseDto> AuthenticateMeFromFormAsync([FromForm] AuthDto auth)
{
return AuthenticateMeAsync(auth);
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
[Create("logout")]
[Read("logout")]// temp fix
public void Logout()
{
if (_securityContext.IsAuthenticated)
_cookiesManager.ResetUserCookie(_securityContext.CurrentAccount.ID);
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
_cookiesManager.ClearCookies(CookiesType.AuthKey);
_cookiesManager.ClearCookies(CookiesType.SocketIO);
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
_securityContext.Logout();
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
[Create("confirm", false)]
public ValidationResult CheckConfirmFromBody([FromBody] EmailValidationKeyModel model)
{
return _emailValidationKeyModelHelper.Validate(model);
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
[Create("confirm", false)]
[Consumes("application/x-www-form-urlencoded")]
public ValidationResult CheckConfirmFromForm([FromForm] EmailValidationKeyModel model)
{
return _emailValidationKeyModelHelper.Validate(model);
}
[Authorize(AuthenticationSchemes = "confirm", Roles = "PhoneActivation")]
[Create("setphone", false)]
public Task<AuthenticationTokenResponseDto> SaveMobilePhoneFromBodyAsync([FromBody] MobileDto model)
{
return SaveMobilePhoneAsync(model);
}
[Authorize(AuthenticationSchemes = "confirm", Roles = "PhoneActivation")]
[Create("setphone", false)]
[Consumes("application/x-www-form-urlencoded")]
public Task<AuthenticationTokenResponseDto> SaveMobilePhoneFromFormAsync([FromForm] MobileDto model)
{
return SaveMobilePhoneAsync(model);
}
private async Task<AuthenticationTokenResponseDto> SaveMobilePhoneAsync(MobileDto model)
{
_apiContext.AuthByClaim();
var user = _userManager.GetUsers(_authContext.CurrentAccount.ID);
model.MobilePhone = await _smsManager.SaveMobilePhoneAsync(user, model.MobilePhone);
_messageService.Send(MessageAction.UserUpdatedMobileNumber, _messageTarget.Create(user.Id), user.DisplayUserName(false, _displayUserSettingsHelper), model.MobilePhone);
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
return new AuthenticationTokenResponseDto
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
Sms = true,
PhoneNoise = SmsSender.BuildPhoneNoise(model.MobilePhone),
Expires = new ApiDateTime(_tenantManager, _timeZoneConverter, DateTime.UtcNow.Add(_smsKeyStorage.StoreInterval))
};
}
[Create(@"sendsms", false)]
public Task<AuthenticationTokenResponseDto> SendSmsCodeFromBodyAsync([FromBody] AuthDto model)
{
return SendSmsCodeAsync(model);
}
[Create(@"sendsms", false)]
[Consumes("application/x-www-form-urlencoded")]
public Task<AuthenticationTokenResponseDto> SendSmsCodeFromFormAsync([FromForm] AuthDto model)
{
return SendSmsCodeAsync(model);
}
private async Task<AuthenticationTokenResponseDto> SendSmsCodeAsync(AuthDto model)
{
var user = GetUser(model, out _);
await _smsManager.PutAuthCodeAsync(user, true);
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
return new AuthenticationTokenResponseDto
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
Sms = true,
PhoneNoise = SmsSender.BuildPhoneNoise(user.MobilePhone),
Expires = new ApiDateTime(_tenantManager, _timeZoneConverter, DateTime.UtcNow.Add(_smsKeyStorage.StoreInterval))
};
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
private async Task<AuthenticationTokenResponseDto> AuthenticateMeAsync(AuthDto auth)
{
bool viaEmail;
var user = GetUser(auth, out viaEmail);
if (_studioSmsNotificationSettingsHelper.IsVisibleSettings() && _studioSmsNotificationSettingsHelper.Enable)
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
if (string.IsNullOrEmpty(user.MobilePhone) || user.MobilePhoneActivationStatus == MobilePhoneActivationStatus.NotActivated)
return new AuthenticationTokenResponseDto
{
Sms = true,
ConfirmUrl = _commonLinkUtility.GetConfirmationUrl(user.Email, ConfirmType.PhoneActivation)
};
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
await _smsManager.PutAuthCodeAsync(user, false);
return new AuthenticationTokenResponseDto
2022-02-28 15:31:03 +00:00
{
Sms = true,
2022-03-01 10:58:02 +00:00
PhoneNoise = SmsSender.BuildPhoneNoise(user.MobilePhone),
Expires = new ApiDateTime(_tenantManager, _timeZoneConverter, DateTime.UtcNow.Add(_smsKeyStorage.StoreInterval)),
ConfirmUrl = _commonLinkUtility.GetConfirmationUrl(user.Email, ConfirmType.PhoneAuth)
2022-02-28 15:31:03 +00:00
};
}
2022-03-01 10:58:02 +00:00
if (TfaAppAuthSettings.IsVisibleSettings && _settingsManager.Load<TfaAppAuthSettings>().EnableSetting)
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
if (!TfaAppUserSettings.EnableForUser(_settingsManager, user.Id))
return new AuthenticationTokenResponseDto
{
Tfa = true,
TfaKey = _tfaManager.GenerateSetupCode(user).ManualEntryKey,
ConfirmUrl = _commonLinkUtility.GetConfirmationUrl(user.Email, ConfirmType.TfaActivation)
};
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
return new AuthenticationTokenResponseDto
{
Tfa = true,
ConfirmUrl = _commonLinkUtility.GetConfirmationUrl(user.Email, ConfirmType.TfaAuth)
};
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
try
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
var token = _securityContext.AuthenticateMe(user.Id);
_cookiesManager.SetCookies(CookiesType.AuthKey, token, auth.Session);
_messageService.Send(viaEmail ? MessageAction.LoginSuccessViaApi : MessageAction.LoginSuccessViaApiSocialAccount);
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
var tenant = _tenantManager.GetCurrentTenant().Id;
var expires = _tenantCookieSettingsHelper.GetExpiresTime(tenant);
return new AuthenticationTokenResponseDto
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
Token = token,
Expires = new ApiDateTime(_tenantManager, _timeZoneConverter, expires)
2022-02-28 15:31:03 +00:00
};
}
2022-03-01 10:58:02 +00:00
catch
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
_messageService.Send(user.DisplayUserName(false, _displayUserSettingsHelper), viaEmail ? MessageAction.LoginFailViaApi : MessageAction.LoginFailViaApiSocialAccount);
throw new AuthenticationException("User authentication failed");
}
finally
{
_securityContext.Logout();
}
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
private AuthenticationTokenResponseDto AuthenticateMeWithCode(AuthDto auth)
{
var tenant = _tenantManager.GetCurrentTenant().Id;
var user = GetUser(auth, out _);
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
var sms = false;
try
{
if (_studioSmsNotificationSettingsHelper.IsVisibleSettings() && _studioSmsNotificationSettingsHelper.Enable)
{
sms = true;
_smsManager.ValidateSmsCode(user, auth.Code);
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
else if (TfaAppAuthSettings.IsVisibleSettings && _settingsManager.Load<TfaAppAuthSettings>().EnableSetting)
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
if (_tfaManager.ValidateAuthCode(user, auth.Code))
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
_messageService.Send(MessageAction.UserConnectedTfaApp, _messageTarget.Create(user.Id));
}
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
else
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
throw new System.Security.SecurityException("Auth code is not available");
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
var token = _securityContext.AuthenticateMe(user.Id);
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
_messageService.Send(sms ? MessageAction.LoginSuccessViaApiSms : MessageAction.LoginSuccessViaApiTfa);
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
var expires = _tenantCookieSettingsHelper.GetExpiresTime(tenant);
var result = new AuthenticationTokenResponseDto
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
Token = token,
Expires = new ApiDateTime(_tenantManager, _timeZoneConverter, expires)
};
if (sms)
{
result.Sms = true;
result.PhoneNoise = SmsSender.BuildPhoneNoise(user.MobilePhone);
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
else
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
result.Tfa = true;
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
return result;
}
catch
{
_messageService.Send(user.DisplayUserName(false, _displayUserSettingsHelper), sms
? MessageAction.LoginFailViaApiSms
: MessageAction.LoginFailViaApiTfa,
_messageTarget.Create(user.Id));
throw new AuthenticationException("User authentication failed");
}
finally
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
_securityContext.Logout();
}
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
private UserInfo GetUser(AuthDto memberModel, out bool viaEmail)
{
viaEmail = true;
var action = MessageAction.LoginFailViaApi;
UserInfo user;
try
{
if ((string.IsNullOrEmpty(memberModel.Provider) && string.IsNullOrEmpty(memberModel.SerializedProfile)) || memberModel.Provider == "email")
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
memberModel.UserName.ThrowIfNull(new ArgumentException(@"userName empty", "userName"));
if (!string.IsNullOrEmpty(memberModel.Password))
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
memberModel.Password.ThrowIfNull(new ArgumentException(@"password empty", "password"));
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
else
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
memberModel.PasswordHash.ThrowIfNull(new ArgumentException(@"PasswordHash empty", "PasswordHash"));
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
int counter;
int.TryParse(_cache.Get<string>("loginsec/" + memberModel.UserName), out counter);
if (++counter > _setupInfo.LoginThreshold && !SetupInfo.IsSecretEmail(memberModel.UserName))
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
throw new BruteForceCredentialException();
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
_cache.Insert("loginsec/" + memberModel.UserName, counter.ToString(CultureInfo.InvariantCulture), DateTime.UtcNow.Add(TimeSpan.FromMinutes(1)));
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
memberModel.PasswordHash = (memberModel.PasswordHash ?? "").Trim();
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
if (string.IsNullOrEmpty(memberModel.PasswordHash))
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
memberModel.Password = (memberModel.Password ?? "").Trim();
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
if (!string.IsNullOrEmpty(memberModel.Password))
{
memberModel.PasswordHash = _passwordHasher.GetClientPassword(memberModel.Password);
}
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
user = _userManager.GetUsersByPasswordHash(
_tenantManager.GetCurrentTenant().Id,
memberModel.UserName,
memberModel.PasswordHash);
if (user == null || !_userManager.UserExists(user))
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
throw new Exception("user not found");
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
_cache.Insert("loginsec/" + memberModel.UserName, (--counter).ToString(CultureInfo.InvariantCulture), DateTime.UtcNow.Add(TimeSpan.FromMinutes(1)));
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
else
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
viaEmail = false;
action = MessageAction.LoginFailViaApiSocialAccount;
LoginProfile thirdPartyProfile;
if (!string.IsNullOrEmpty(memberModel.SerializedProfile))
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
thirdPartyProfile = new LoginProfile(_signature, _instanceCrypto, memberModel.SerializedProfile);
2022-02-28 15:31:03 +00:00
}
else
{
2022-03-01 10:58:02 +00:00
thirdPartyProfile = _providerManager.GetLoginProfile(memberModel.Provider, memberModel.AccessToken);
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
memberModel.UserName = thirdPartyProfile.EMail;
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
user = GetUserByThirdParty(thirdPartyProfile);
2022-02-28 15:31:03 +00:00
}
}
2022-03-01 10:58:02 +00:00
catch (BruteForceCredentialException)
{
_messageService.Send(!string.IsNullOrEmpty(memberModel.UserName) ? memberModel.UserName : AuditResource.EmailNotSpecified, MessageAction.LoginFailBruteForce);
throw new AuthenticationException("Login Fail. Too many attempts");
}
catch
{
_messageService.Send(!string.IsNullOrEmpty(memberModel.UserName) ? memberModel.UserName : AuditResource.EmailNotSpecified, action);
throw new AuthenticationException("User authentication failed");
}
return user;
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
private UserInfo GetUserByThirdParty(LoginProfile loginProfile)
{
try
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
if (!string.IsNullOrEmpty(loginProfile.AuthorizationError))
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
// ignore cancellation
if (loginProfile.AuthorizationError != "Canceled at provider")
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
throw new Exception(loginProfile.AuthorizationError);
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
return Constants.LostUser;
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
var userInfo = Constants.LostUser;
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
Guid userId;
if (TryGetUserByHash(loginProfile.HashId, out userId))
{
userInfo = _userManager.GetUsers(userId);
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
var isNew = false;
if (_coreBaseSettings.Personal)
{
if (_userManager.UserExists(userInfo.Id) && SetupInfo.IsSecretEmail(userInfo.Email))
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
try
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
_securityContext.AuthenticateMeWithoutCookie(ASC.Core.Configuration.Constants.CoreSystem);
_userManager.DeleteUser(userInfo.Id);
userInfo = Constants.LostUser;
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
finally
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
_securityContext.Logout();
2022-02-28 15:31:03 +00:00
}
}
2022-03-01 10:58:02 +00:00
if (!_userManager.UserExists(userInfo.Id))
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
userInfo = JoinByThirdPartyAccount(loginProfile);
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
isNew = true;
}
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
if (isNew)
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
//TODO:
//var spam = HttpContext.Current.Request["spam"];
//if (spam != "on")
//{
// try
// {
// const string _databaseID = "com";
// using (var db = DbManager.FromHttpContext(_databaseID))
// {
// db.ExecuteNonQuery(new SqlInsert("template_unsubscribe", false)
// .InColumnValue("email", userInfo.Email.ToLowerInvariant())
// .InColumnValue("reason", "personal")
// );
// Log.Debug(string.Format("Write to template_unsubscribe {0}", userInfo.Email.ToLowerInvariant()));
// }
// }
// catch (Exception ex)
// {
// Log.Debug(string.Format("ERROR write to template_unsubscribe {0}, email:{1}", ex.Message, userInfo.Email.ToLowerInvariant()));
// }
//}
_studioNotifyService.UserHasJoin();
_userHelpTourHelper.IsNewUser = true;
_personalSettingsHelper.IsNewUser = true;
2022-02-28 15:31:03 +00:00
}
return userInfo;
}
2022-03-01 10:58:02 +00:00
catch (Exception)
{
_cookiesManager.ClearCookies(CookiesType.AuthKey);
_cookiesManager.ClearCookies(CookiesType.SocketIO);
_securityContext.Logout();
throw;
}
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
private UserInfo JoinByThirdPartyAccount(LoginProfile loginProfile)
{
if (string.IsNullOrEmpty(loginProfile.EMail))
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
throw new Exception(Resource.ErrorNotCorrectEmail);
}
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
var userInfo = _userManager.GetUserByEmail(loginProfile.EMail);
if (!_userManager.UserExists(userInfo.Id))
{
var newUserInfo = ProfileToUserInfo(loginProfile);
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
try
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
_securityContext.AuthenticateMeWithoutCookie(ASC.Core.Configuration.Constants.CoreSystem);
userInfo = _userManagerWrapper.AddUser(newUserInfo, UserManagerWrapper.GeneratePassword());
}
finally
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
_securityContext.Logout();
2022-02-28 15:31:03 +00:00
}
}
2022-03-01 10:58:02 +00:00
var linker = _accountLinker.Get("webstudio");
linker.AddLink(userInfo.Id.ToString(), loginProfile);
return userInfo;
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
private UserInfo ProfileToUserInfo(LoginProfile loginProfile)
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
if (string.IsNullOrEmpty(loginProfile.EMail)) throw new Exception(Resource.ErrorNotCorrectEmail);
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
var firstName = loginProfile.FirstName;
if (string.IsNullOrEmpty(firstName)) firstName = loginProfile.DisplayName;
2022-02-28 15:31:03 +00:00
2022-03-01 10:58:02 +00:00
var userInfo = new UserInfo
2022-02-28 15:31:03 +00:00
{
2022-03-01 10:58:02 +00:00
FirstName = string.IsNullOrEmpty(firstName) ? UserControlsCommonResource.UnknownFirstName : firstName,
LastName = string.IsNullOrEmpty(loginProfile.LastName) ? UserControlsCommonResource.UnknownLastName : loginProfile.LastName,
Email = loginProfile.EMail,
Title = string.Empty,
Location = string.Empty,
CultureName = _coreBaseSettings.CustomMode ? "ru-RU" : Thread.CurrentThread.CurrentUICulture.Name,
ActivationStatus = EmployeeActivationStatus.Activated,
};
var gender = loginProfile.Gender;
if (!string.IsNullOrEmpty(gender))
{
userInfo.Sex = gender == "male";
2022-02-28 15:31:03 +00:00
}
2022-03-01 10:58:02 +00:00
return userInfo;
}
private bool TryGetUserByHash(string hashId, out Guid userId)
{
userId = Guid.Empty;
if (string.IsNullOrEmpty(hashId)) return false;
var linkedProfiles = _accountLinker.Get("webstudio").GetLinkedObjectsByHashId(hashId);
var tmp = Guid.Empty;
if (linkedProfiles.Any(profileId => Guid.TryParse(profileId, out tmp) && _userManager.UserExists(tmp)))
userId = tmp;
return true;
2022-02-28 15:31:03 +00:00
}
2019-05-28 09:40:37 +00:00
}