private string GetOwnerUserExternalId(CommonPart commonPart) { // assignee user if (commonPart.Owner != null) { var user = this.services.ContentManager.Get <IUser>(commonPart.Owner.Id); if (user != null) { SuiteCRMUserPart userPart = user.As <SuiteCRMUserPart>(); if (userPart != null && !string.IsNullOrEmpty(userPart.ExternalId)) { return(userPart.ExternalId); } } } return(null); }
private string GetAssigneeUserExternalId(ContentItemPermissionPart sourcePermissionPart) { // assignee user var assigneeUser = sourcePermissionPart.Record.Items.FirstOrDefault(c => c.AccessType == ContentItemPermissionAccessTypes.Assignee && c.User != null); if (assigneeUser != null) { var user = this.services.ContentManager.Get <IUser>(assigneeUser.User.Id); if (user != null) { SuiteCRMUserPart userPart = user.As <SuiteCRMUserPart>(); if (userPart != null && !string.IsNullOrEmpty(userPart.ExternalId)) { return(userPart.ExternalId); } } } return(null); }
public IEnumerable <SuiteCRMUserViewModel> CopySuiteCRMUsersToOrchard(CopySuiteCRMUsersToOrchardViewModel model) { List <SuiteCRMUserViewModel> returnValue = new List <SuiteCRMUserViewModel>(); using (var connection = Helper.GetConnection(this.services, this.Logger)) using (SuiteCRMUserUnitOfWork userRepository = new SuiteCRMUserUnitOfWork(connection)) using (SuiteCRMEmailAddressUnitOfWork emailRepository = new SuiteCRMEmailAddressUnitOfWork(connection)) using (SuiteCRMEmailAddressBeanUnitOfWork emailBeanRepository = new SuiteCRMEmailAddressBeanUnitOfWork(connection)) { var ids = model.Users.Where(c => !string.IsNullOrEmpty(c.SuiteCRMUserId)).Select(c => c.SuiteCRMUserId).ToArray(); var suiteUsers = userRepository.GetUsers(ids); var suiteUserEmailBeans = emailBeanRepository.GetEmailAddressesBean(UsersBean, ids).ToArray(); var suiteUserEmails = emailRepository.GetEmailAddresses(suiteUserEmailBeans.Select(c => c.email_address_id).ToArray()).ToList(); var orchardUsers = this.services .ContentManager .GetMany <IUser>(model.Users.Where(c => c.OrchardUserId.HasValue).Select(c => c.OrchardUserId.Value), VersionOptions.Published, new QueryHints()); var operatorRole = this.GetOperatorRole(); foreach (var item in model.Users.Where(c => !string.IsNullOrEmpty(c.SuiteCRMUserId))) { var suiteCRMUser = suiteUsers.FirstOrDefault(c => c.id == item.SuiteCRMUserId); if (suiteCRMUser == null) { continue; } var suiteCRMEmailBean = suiteUserEmailBeans.FirstOrDefault(c => c.bean_id == item.SuiteCRMUserId); if (suiteCRMEmailBean == null) { continue; } var suiteCRMUserEmail = suiteUserEmails.FirstOrDefault(c => c.id == suiteCRMEmailBean.email_address_id); if (suiteCRMUserEmail == null) { continue; } IUser user = orchardUsers.FirstOrDefault(c => c.UserName.ToLower() == suiteCRMUser.user_name.ToLower() || c.Email.ToLower() == suiteCRMUserEmail.email_address.ToLower()); if (user != null) { continue; } var newUser = this.membershipService.CreateUser(new CreateUserParams( suiteCRMUser.user_name, model.DefaultPassword, suiteCRMUserEmail.email_address, null, null, true)); SuiteCRMUserPart suiteCRMUserPart = newUser.As <SuiteCRMUserPart>(); suiteCRMUserPart.ExternalId = suiteCRMUser.id; suiteCRMUserPart.LastSyncTime = DateTime.UtcNow; // Full name var userPart = newUser.ContentItem.Parts.FirstOrDefault(d => d.PartDefinition.Name.ToLower(CultureInfo.InvariantCulture) == "user"); userPart.Store("FullName", string.Format("{0} {1}", suiteCRMUser.first_name, suiteCRMUser.last_name)); // role if (operatorRole != null) { UserRolesPartRecord newUserRole = new UserRolesPartRecord { UserId = newUser.Id, Role = new RoleRecord { Id = operatorRole.Id } }; this.userRolesPartRecordRepository.Create(newUserRole); } this.services.ContentManager.Publish(newUser.ContentItem); this.userRolesPartRecordRepository.Flush(); returnValue.Add(new SuiteCRMUserViewModel { IsSync = true, OrchardEmail = newUser.Email, OrchardUserId = newUser.Id, SuiteCRMUserId = suiteCRMUser.id, SuiteCRMEmail = suiteCRMUserEmail.email_address, SuiteCRMUsername = suiteCRMUser.user_name, OrchardUsername = newUser.UserName }); } } return(returnValue); }