public override void WithResolvingModelHost(object modelHost, DefinitionBase model, Type childModelType, Action<object> action)
        {
            var securableObject = ExtractSecurableObject(modelHost);

            if (securableObject is SecurableObject)
            {
                var securityGroupLinkModel = model as SecurityGroupLinkDefinition;
                if (securityGroupLinkModel == null) throw new ArgumentException("model has to be SecurityGroupDefinition");

                var web = GetWebFromSPSecurableObject(securableObject as SecurableObject);

                var context = web.Context;

                context.Load(web, w => w.SiteGroups);
                context.ExecuteQueryWithTrace();

                Group securityGroup = ResolveSecurityGroup(securityGroupLinkModel, web, context);

                var newModelHost = new SecurityGroupModelHost
                {
                    SecurableObject = securableObject,
                    SecurityGroup = securityGroup
                };

                action(newModelHost);
            }
            else
            {
                action(modelHost);
            }
        }
        public override void WithResolvingModelHost(object modelHost, DefinitionBase model, Type childModelType, Action<object> action)
        {
            if (modelHost is SecurableObject)
            {
                var securityGroupLinkModel = model as SecurityGroupLinkDefinition;
                if (securityGroupLinkModel == null) throw new ArgumentException("model has to be SecurityGroupDefinition");

                var web = GetWebFromSPSecurableObject(modelHost as SecurableObject);

                var context = web.Context;

                context.Load(web, w => w.SiteGroups);
                context.ExecuteQuery();

                var securityGroup = WebExtensions.FindGroupByName(web.SiteGroups, securityGroupLinkModel.SecurityGroupName);

                var newModelHost = new SecurityGroupModelHost
                {
                    SecurableObject = modelHost as SecurableObject,
                    SecurityGroup = securityGroup
                };

                action(newModelHost);
            }
            else
            {
                action(modelHost);
            }
        }
        private void DeploySiteGroupUnderSiteGroup(object modelHost, SecurityGroupModelHost securityGroupModelHost, DefinitionBase model)
        {
            var securityGroupModel = model.WithAssertAndCast<SecurityGroupDefinition>("model",
              value => value.RequireNotNull());

            var context = securityGroupModelHost.HostClientContext;

            var currentGroup = securityGroupModelHost.SecurityGroup;
            var subGroup = securityGroupModelHost.HostWeb.EnsureUser(securityGroupModel.Name);

            context.Load(subGroup);
            context.Load(currentGroup, g => g.Users);

            context.ExecuteQueryWithTrace();

            var existingGroup = currentGroup.Users.FirstOrDefault(u => u.Id == subGroup.Id);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = currentGroup,
                ObjectType = typeof(Principal),
                ObjectDefinition = securityGroupModel,
                ModelHost = modelHost
            });

            if (existingGroup == null)
            {
                existingGroup = currentGroup.Users.Add(new UserCreationInformation
                {
                    Title = subGroup.Title,
                    LoginName = subGroup.LoginName,
                    Email = subGroup.Email
                });
            }

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioned,
                Object = currentGroup,
                ObjectType = typeof(Principal),
                ObjectDefinition = securityGroupModel,
                ModelHost = modelHost
            });

            if (existingGroup == null)
            {
                currentGroup.Update();
                context.ExecuteQueryWithTrace();
            }
        }