Exemple #1
0
        public ActionResult CopySuiteCRMUsersToOrchardPost(CopySuiteCRMUsersToOrchardViewModel model)
        {
            if (!this.services.Authorizer.Authorize(Permissions.ManageSuiteCRMPermission, T("Not authorized to see list of SuiteCRM projects")))
            {
                return(new HttpUnauthorizedResult());
            }

            if (!this.ModelState.IsValid)
            {
                AjaxMessageViewModel errorModel = this.CreateAjaxModel(null);
                return(this.Json(errorModel, JsonRequestBehavior.AllowGet));
            }

            var users = this.suiteCRMSyncUserService.CopySuiteCRMUsersToOrchard(model);

            AjaxMessageViewModel ajaxMessageModel = this.CreateAjaxModel(users);

            return(this.Json(JsonConvert.SerializeObject(ajaxMessageModel)));
        }
        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);
        }