public void AddUserIntoGroup(Guid userId, Guid groupId) { if (Constants.LostUser.ID == userId || Constants.LostGroupInfo.ID == groupId) { return; } SecurityContext.DemandPermissions(Constants.Action_EditGroups); userService.SaveUserGroupRef( CoreContext.TenantManager.GetCurrentTenant().TenantId, new UserGroupRef(userId, groupId, UserGroupRefType.Contains)); GetUsers(userId).ResetGroupCache(); var user = CoreContext.UserManager.GetUsers(userId); if (groupId == Constants.GroupVisitor.ID) { var tenant = CoreContext.TenantManager.GetCurrentTenant(); var myUri = (HttpContext.Current != null) ? HttpContext.Current.Request.GetUrlRewriter().ToString() : (Cache.Get <string>("REWRITE_URL" + tenant.TenantId) != null) ? new Uri(Cache.Get <string>("REWRITE_URL" + tenant.TenantId)).ToString() : tenant.GetTenantDomain(); var cardDavAB = new CardDavAddressbook(); cardDavAB.Delete(myUri, user.ID, user.Email, tenant.TenantId); } }
public UserInfo SaveUserInfo(UserInfo u, bool isVisitor = false, bool syncCardDav = false) { if (IsSystemUser(u.ID)) { return(systemUsers[u.ID]); } if (u.ID == Guid.Empty) { SecurityContext.DemandPermissions(Constants.Action_AddRemoveUser); } else { SecurityContext.DemandPermissions(new UserSecurityProvider(u.ID), Constants.Action_EditUser); } if (!CoreContext.Configuration.Personal) { if (Constants.MaxEveryoneCount <= GetUsersByGroup(Constants.GroupEveryone.ID).Length) { throw new TenantQuotaException("Maximum number of users exceeded"); } if (u.Status == EmployeeStatus.Active) { if (isVisitor) { var maxUsers = CoreContext.TenantManager.GetTenantQuota(CoreContext.TenantManager.GetCurrentTenant().TenantId).ActiveUsers; if (!CoreContext.Configuration.Standalone && CoreContext.UserManager.GetUsersByGroup(Constants.GroupVisitor.ID).Length > Constants.CoefficientOfVisitors * maxUsers) { throw new TenantQuotaException("Maximum number of visitors exceeded"); } } else { var q = CoreContext.TenantManager.GetTenantQuota(CoreContext.TenantManager.GetCurrentTenant().TenantId); if (q.ActiveUsers < GetUsersByGroup(Constants.GroupUser.ID).Length) { throw new TenantQuotaException(string.Format("Exceeds the maximum active users ({0})", q.ActiveUsers)); } } } } if (u.Status == EmployeeStatus.Terminated && u.ID == CoreContext.TenantManager.GetCurrentTenant().OwnerId) { throw new InvalidOperationException("Can not disable tenant owner."); } var oldUserData = userService.GetUserByUserName(CoreContext.TenantManager.GetCurrentTenant().TenantId, u.UserName); var newUser = userService.SaveUser(CoreContext.TenantManager.GetCurrentTenant().TenantId, u); if (syncCardDav) { var tenant = CoreContext.TenantManager.GetCurrentTenant(); var cardDavAB = new CardDavAddressbook(); var myUri = (HttpContext.Current != null) ? HttpContext.Current.Request.GetUrlRewriter().ToString() : (Cache.Get <string>("REWRITE_URL" + tenant.TenantId) != null) ? new Uri(Cache.Get <string>("REWRITE_URL" + tenant.TenantId)).ToString() : tenant.GetTenantDomain(); var rootAuthorization = cardDavAB.GetSystemAuthorization(); var allUserEmails = CoreContext.UserManager.GetDavUserEmails().ToList(); var cardDavAddBook = new CardDavAddressbook(); if (oldUserData != null && oldUserData.Status != newUser.Status && newUser.Status == EmployeeStatus.Terminated) { var userAuthorization = oldUserData.Email.ToLower() + ":" + InstanceCrypto.Encrypt(oldUserData.Email); var requestUrlBook = cardDavAB.GetRadicaleUrl(myUri, newUser.Email.ToLower(), true, true); var collection = cardDavAB.GetCollection(requestUrlBook, userAuthorization, myUri.ToString()).Result; if (collection.Completed && collection.StatusCode != 404) { cardDavAB.Delete(myUri, newUser.ID, newUser.Email, tenant.TenantId); } foreach (string email in allUserEmails) { var requestUrlItem = cardDavAddBook.GetRadicaleUrl(myUri.ToString(), email.ToLower(), true, true, itemID: newUser.ID.ToString()); try { var davItemRequest = new DavRequest() { Url = requestUrlItem, Authorization = rootAuthorization, Header = myUri }; RadicaleClient.RemoveAsync(davItemRequest).ConfigureAwait(false); } catch (Exception ex) { LogManager.GetLogger("ASC").Error("ERROR: " + ex.Message); } } } else { try { var cardDavUser = new CardDavItem(u.ID, u.FirstName, u.LastName, u.UserName, u.BirthDate, u.Sex, u.Title, u.Email, u.Contacts, u.MobilePhone); try { cardDavAB.UpdateItemForAllAddBooks(allUserEmails, myUri, cardDavUser, CoreContext.TenantManager.GetCurrentTenant().TenantId, oldUserData != null && oldUserData.Email != newUser.Email ? oldUserData.Email : null); } catch (Exception ex) { LogManager.GetLogger("ASC").Error("ERROR: " + ex.Message); } } catch (Exception ex) { LogManager.GetLogger("ASC").Error("ERROR: " + ex.Message); } } } return(newUser); }