public override void Associate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)
 {
     if (AssociateAction != null)
     {
         AssociateAction(Service, entityName, entityId, relationship, relatedEntities);
     }
     else if (AssociateActions != null)
     {
         if (AssociateCache == null)
         {
             AssociateCache = Service;
             foreach (var action in AssociateActions)
             {
                 AssociateCache = new FakeIOrganizationService(AssociateCache)
                 {
                     AssociateAction = action
                 };
             }
         }
         AssociateCache.Associate(entityName, entityId, relationship, relatedEntities);
     }
     else
     {
         if (ExecutionTracingEnabled)
         {
             Timer.Time(AssociateInternal, new Tuple <string, Guid, Relationship, EntityReferenceCollection>(entityName, entityId, relationship, relatedEntities),
                        "Associate {0}:{1} using relationship {2} to releated Entities {3}: {4}",
                        entityName, entityId, relationship, relatedEntities.Select(e => e.GetNameId()).ToCsv());
         }
         else
         {
             Service.Associate(entityName, entityId, relationship, relatedEntities);
         }
     }
 }
Ejemplo n.º 2
0
        private bool createUser(string firstname, string lastname, string fullname, string username, string email, string role, string group, string company, string license, DataGridViewRow item)
        {
            bool result = false;

            try
            {
                // find person by firstname, lastname & owner contact
                SuperOffice.CRM.Entities.Person.CustomSearch pc = new SuperOffice.CRM.Entities.Person.CustomSearch();
                pc.Restriction = pc.TableInfo.Firstname.Equal(S.Parameter(firstname)).
                                 And(pc.TableInfo.Lastname.Equal(S.Parameter(lastname))).
                                 And(pc.TableInfo.ContactId.Equal(S.Parameter(_contacts[company])));

                SuperOffice.CRM.Entities.Person p = SuperOffice.CRM.Entities.Person.GetFromCustomSearch(pc);

                // we either found an existing person, or got a blank, ready-to-populate one
                if (p.IsNew)
                {
                    p.SetDefaults(_contacts[company]);

                    p.Firstname = firstname;
                    p.Lastname  = lastname;
                }

                // always set userid into number field, for convenience
                p.PersonNumber = username;

                // find existing email, or create a new one
                EmailRow em = null;
                if (p.Emails.Count == 0)
                {
                    em = p.Emails.AddNew();
                    em.SetDefaults();
                }
                else
                {
                    em = p.Emails[0];
                }

                // always set correct email; we have just the one address
                em.EmailAddress = email;
                em.Protocol     = "SMTP";

                // save complete person entity
                p.Save();

                // if person is associate - get him/her; otherwise create a new SoUser
                SoUser user;
                if (AssociateCache.GetCurrent().IsPersonAssociate(p.PersonId))
                {
                    user = SoUser.ManageUserFromPersonId(p.PersonId)[0];
                }
                else
                {
                    user = SoUser.CreateNew(p.PersonId, UserType.InternalAssociate);
                }

                // set our various properties
                user.SetPassword(username);
                user.GroupIdx      = _groups[group];
                user.OtherGroupIds = new int[0];

                user.RoleIdx   = _roles[role];
                user.LogonName = username;
                user.Tooltip   = fullname + " (" + company + ")";

                // add licenses
                if (user.GetModuleLicense("SuperOffice", DefaultLicense).CanAssign)
                {
                    user.GetModuleLicense("SuperOffice", DefaultLicense).Assigned = true;
                    item.Cells["AssignedLicenses"].Value = "Assigned Default";
                }
                else
                {
                    item.Cells["AssignedLicenses"].Value = "Cannot Assign";
                }
                //user.GetModuleLicense(SoLicenseNames.SuperLicenseServicePro).Assigned = true;

                /*user.GetModuleLicense(SoLicenseNames.User).Assigned = true;
                 * user.GetModuleLicense(SoLicenseNames.Web).Assigned = true;*/
                user.GetModuleLicense(SoLicenseNames.VisibleFor).Assigned = true;

                // save the user
                user.Save();
                result = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return(result);
        }