public override void ItemAdded(SPItemEventProperties properties)
 {
     if (Initialize(properties))
     {
         if (_settings.BuildTeamSecurity)
         {
             WorkspaceTimerjobAgent.AddCreateWorkspaceJobAndWait(_creationParams);
         }
         else
         {
             WorkspaceTimerjobAgent.AddAndQueueCreateWorkspaceJob(_creationParams);
         }
     }
 }
        public void AddCreateWorkspaceJob_ValidXml_ReturnsSuccess()
        {
            using (TestCheck.OpenCloseConnections)
            {
                // Arrange
                ArrangeCreateJob();

                // Act
                var result = WorkspaceTimerjobAgent.AddCreateWorkspaceJob(SampleXml);

                // Assert
                this.ShouldSatisfyAllConditions(
                    () => _actualSqlCommand.ShouldBe(ExpectedSqlCommandCreateJob),
                    () => _actualParameterList.All(parameter => _expectedParameterList.Contains(parameter)),
                    () => result.Contains("success"));
            }
        }
        public void AddAndQueueCreateWorkspaceJob_ValidXml_ReturnsSuccessAndEnqueueJob()
        {
            using (TestCheck.OpenCloseConnections)
            {
                // Arrange
                ArrangeCreateJob();
                var enqueueWasCalled = false;
                ShimCoreFunctions.enqueueGuidInt32SPSite = (_1, _2, _3) => enqueueWasCalled = true;

                // Act
                var result = WorkspaceTimerjobAgent.AddAndQueueCreateWorkspaceJob(SampleXml);

                // Assert
                this.ShouldSatisfyAllConditions(
                    () => _actualSqlCommand.ShouldBe(ExpectedSqlCommandCreateJob),
                    () => _actualParameterList.All(parameter => _expectedParameterList.Contains(parameter)),
                    () => enqueueWasCalled.ShouldBeTrue(),
                    () => result.Contains("success"));
            }
        }
        public void execute(SPSite site, SPWeb web, Guid listId, int itemId, int userid, string data)
        {
            SPList            list        = null;
            SPListItem        li          = null;
            GridGanttSettings settings    = null;
            List <string>     cNewGrps    = null;
            List <string>     fields      = null;
            SPUser            orignalUser = null;
            EnhancedLookupConfigValuesHelper valueHelper = null;
            SPList             lookupPrntList            = null;
            GridGanttSettings  prntListSettings          = null;
            SPFieldLookupValue lookupVal  = null;
            SPListItem         targetItem = null;

            try
            {
                list     = web.Lists[listId];
                li       = list.GetItemById(itemId);
                settings = new GridGanttSettings(list);
                cNewGrps = new List <string>();
                bool isSecure = false;
                try
                {
                    isSecure = settings.BuildTeamSecurity;
                }
                catch { }

                orignalUser = web.AllUsers.GetByID(userid);

                string safeTitle = !string.IsNullOrEmpty(li.Title) ? GetSafeGroupTitle(li.Title) : string.Empty;

                if (string.IsNullOrEmpty(safeTitle) && list.BaseTemplate == SPListTemplateType.DocumentLibrary)
                {
                    safeTitle = GetSafeGroupTitle(li.Name); //Assign Name instead of Title - This should perticularly happen with Document libraries.
                }
                if (isSecure)
                {
                    if (!li.HasUniqueRoleAssignments)
                    {
                        web.AllowUnsafeUpdates = true;
                        safeGroupTitle         = safeTitle;

                        safeTitle = GetIdenticalGroupName(site.ID, web.ID, safeTitle, 0);

                        // step 1 perform actions related to "parent item"
                        // ===============================================
                        Dictionary <string, SPRoleType> pNewGrps = null;
                        try
                        {
                            pNewGrps = AddBasicSecurityGroups(web, safeTitle, orignalUser, li);

                            li.BreakRoleInheritance(false);

                            foreach (KeyValuePair <string, SPRoleType> group in pNewGrps)
                            {
                                SPGroup g = web.SiteGroups[group.Key];
                                AddNewItemLvlPerm(li, web, group.Value, g);
                                g = null;
                            }

                            AddBuildTeamSecurityGroups(web, settings, li);
                        }
                        catch { }
                        finally
                        {
                            pNewGrps = null;
                        }
                    }
                }


                // step 2 perform actions related to "child item"
                // ====================================
                // find lookups that has security enabled
                string lookupSettings = settings.Lookups;
                //string rawValue = "Region^dropdown^none^none^xxx|State^autocomplete^Region^Region^xxx|City^autocomplete^State^State^xxx";
                valueHelper = new EnhancedLookupConfigValuesHelper(lookupSettings);

                if (valueHelper == null)
                {
                    return;
                }

                fields = valueHelper.GetSecuredFields();

                bool bHasLookup = false;

                foreach (string fld in fields)
                {
                    SPFieldLookup lookup = null;
                    try
                    {
                        lookup = list.Fields.GetFieldByInternalName(fld) as SPFieldLookup;
                    }
                    catch { }

                    if (lookup == null)
                    {
                        continue;
                    }

                    lookupPrntList   = web.Lists[new Guid(lookup.LookupList)];
                    prntListSettings = new GridGanttSettings(lookupPrntList);
                    string sVal = string.Empty;
                    try
                    {
                        sVal = li[fld].ToString();
                    }
                    catch { }
                    if (!string.IsNullOrEmpty(sVal))
                    {
                        bHasLookup = true;
                        break;
                    }
                }
                if (bHasLookup)
                {
                    // has security fields
                    if (fields.Count > 0)
                    {
                        // if the list is not a security list itself
                        if (isSecure)
                        {
                            li.BreakRoleInheritance(false);
                        }

                        foreach (string fld in fields)
                        {
                            SPFieldLookup lookup = null;
                            try
                            {
                                lookup = list.Fields.GetFieldByInternalName(fld) as SPFieldLookup;
                            }
                            catch { }

                            if (lookup == null)
                            {
                                continue;
                            }

                            lookupPrntList   = web.Lists[new Guid(lookup.LookupList)];
                            prntListSettings = new GridGanttSettings(lookupPrntList);
                            bool isEnableSecurity = false;
                            bool isParentSecure   = false;
                            try
                            {
                                isParentSecure = prntListSettings.BuildTeamSecurity;
                            }
                            catch { }

                            string[]  LookupArray = settings.Lookups.Split('|');
                            string[]  sLookupInfo = null;
                            Hashtable hshLookups  = new Hashtable();
                            foreach (string sLookup in LookupArray)
                            {
                                if (sLookup != "")
                                {
                                    sLookupInfo = sLookup.Split('^');
                                    hshLookups.Add(sLookupInfo[0], sLookupInfo);
                                }
                            }
                            try
                            {
                                if (sLookupInfo != null && sLookupInfo[4].ToLower() == "true")
                                {
                                    isEnableSecurity = true;
                                }
                                else
                                {
                                    isEnableSecurity = false;
                                }
                            }
                            catch { isEnableSecurity = false; }

                            // skip fields with empty lookup values
                            string sVal = string.Empty;
                            try { sVal = li[fld].ToString(); }
                            catch { }
                            if (string.IsNullOrEmpty(sVal))
                            {
                                continue;
                            }

                            lookupVal  = new SPFieldLookupValue(sVal.ToString());
                            targetItem = lookupPrntList.GetItemById(lookupVal.LookupId);
                            if (!targetItem.HasUniqueRoleAssignments)
                            {
                                continue;
                            }
                            else
                            {
                                //EPML-4422: When a project is not using unique security, and a child list like tasks is set to Inherit security from the project lookup, It sets the task to unique, but does not add any groups. It should not get set to Unique.
                                if (!isSecure && isParentSecure && isEnableSecurity)
                                {
                                    web.AllowUnsafeUpdates = true;
                                    li.BreakRoleInheritance(false);
                                }
                            }

                            SPRoleAssignmentCollection raCol = targetItem.RoleAssignments;
                            string itemMemberGrp             = "Member";
                            foreach (SPRoleAssignment ra in raCol)
                            {
                                // add their groups to this item but change permission lvl
                                if (ra.Member.Name.Contains(itemMemberGrp))
                                {
                                    SPRoleAssignment newRa  = new SPRoleAssignment(ra.Member);
                                    SPRoleDefinition newDef = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                    newRa.RoleDefinitionBindings.Add(newDef);
                                    li.RoleAssignments.Add(newRa);
                                }
                                else
                                {
                                    li.RoleAssignments.Add(ra);
                                }

                                cNewGrps.Add(ra.Member.Name);
                            }
                        }
                    }
                }
                ProcessSecurity(site, list, li, userid);

                // we wait until all groups have been created to createworkspace
                // only if there isn't a current process creating ws
                WorkspaceTimerjobAgent.QueueWorkspaceJobOnHoldForSecurity(site.ID, web.ID, list.ID, li.ID);
            }

            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                list             = null;
                li               = null;
                lookupPrntList   = null;
                prntListSettings = null;
                settings         = null;
                cNewGrps         = null;
                fields           = null;
                orignalUser      = null;
                valueHelper      = null;
                lookupVal        = null;
                targetItem       = null;
                fields           = null;
                if (web != null)
                {
                    web.Dispose();
                }
                if (site != null)
                {
                    site.Dispose();
                }
                data = null;
            }
        }