private void AddUser(ClaimsPrincipal claimsPrincipal, UserInfo userInfo) { try { _log.DebugFormat("Adding or updating user in database, userId={0}", userInfo.ID); if (string.IsNullOrWhiteSpace(userInfo.FirstName)) { userInfo.FirstName = Resource.FirstName; } if (string.IsNullOrWhiteSpace(userInfo.LastName)) { userInfo.LastName = Resource.LastName; } SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem); if (!string.IsNullOrEmpty(userInfo.MobilePhone)) { userInfo.MobilePhone = SmsManager.GetPhoneValueDigits(userInfo.MobilePhone); } Claim roleClaim = claimsPrincipal.FindFirst(ClaimTypes.Role); string role = roleClaim != null ? roleClaim.Value : string.Empty; if (SupportedRoles.IsSupported(role) && role != string.Empty) { _log.ErrorFormat("Unknown user role: {0}", role); } if (string.IsNullOrEmpty(userInfo.UserName)) { userInfo.ID = Guid.NewGuid(); if (role == SupportedRoles.Guest || TenantStatisticsProvider.GetUsersCount() >= TenantExtra.GetTenantQuota().ActiveUsers) { userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, true); } else { userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false); } if (role == SupportedRoles.Admin && !CoreContext.UserManager.IsUserInGroup(userInfo.ID, Constants.GroupAdmin.ID)) { CoreContext.UserManager.AddUserIntoGroup(userInfo.ID, Constants.GroupAdmin.ID); } } else { CoreContext.UserManager.SaveUserInfo(userInfo); } Claim photoUrlClaim = claimsPrincipal.FindFirst(SupportedClaimTypes.RemotePhotoUrl); if (photoUrlClaim != null && !string.IsNullOrEmpty(photoUrlClaim.Value)) { var photoLoader = new UserPhotoLoader(); photoLoader.SaveOrUpdatePhoto(photoUrlClaim.Value, userInfo.ID); } } finally { SecurityContext.Logout(); } var cookiesKey = SecurityContext.AuthenticateMe(userInfo.ID); CookiesManager.SetCookies(CookiesType.AuthKey, cookiesKey); }
private void AddUser(SamlResponse samlResponse, UserInfo userInfo) { try { _log.DebugFormat("Adding or updating user in database, userId={0}", userInfo.ID); SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem); if (!string.IsNullOrEmpty(userInfo.MobilePhone)) { userInfo.MobilePhone = SmsManager.GetPhoneValueDigits(userInfo.MobilePhone); } if (string.IsNullOrEmpty(userInfo.UserName)) { if (string.IsNullOrWhiteSpace(userInfo.FirstName)) { userInfo.FirstName = Resource.FirstName; } if (string.IsNullOrWhiteSpace(userInfo.LastName)) { userInfo.LastName = Resource.LastName; } if (TenantStatisticsProvider.GetUsersCount() < TenantExtra.GetTenantQuota().ActiveUsers) { userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false); } else { userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, true); } } else { CoreContext.UserManager.SaveUserInfo(userInfo); } var photoUrl = samlResponse.GetRemotePhotoUrl(); if (!string.IsNullOrEmpty(photoUrl)) { var photoLoader = new UserPhotoLoader(); photoLoader.SaveOrUpdatePhoto(photoUrl, userInfo.ID); } } finally { SecurityContext.Logout(); } var cookiesKey = SecurityContext.AuthenticateMe(userInfo.ID); CookiesManager.SetCookies(CookiesType.AuthKey, cookiesKey); }