<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Framework.Provisioning.Model;
using User = OfficeDevPnP.Core.Framework.Provisioning.Model.User;
using OfficeDevPnP.Core.Diagnostics;
using Microsoft.SharePoint.Client.Utilities;
using OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers.TokenDefinitions;
using RoleDefinition = Microsoft.SharePoint.Client.RoleDefinition;
using OfficeDevPnP.Core.Utilities;
using OfficeDevPnP.Core.Extensions;

namespace OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers
{
    internal class ObjectSiteSecurity : ObjectHandlerBase
    {
        public override string Name
        {
            get { return "Site Security"; }
        }

        public override string InternalName => "SiteSecurity";

        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                // Changed by <NAME> to embrace the new sub-site attributes to break role inheritance and copy role assignments
                // if this is a sub site then we're not provisioning security as by default security is inherited from the root site
                //if (web.IsSubSite() && !template.Security.BreakRoleInheritance)
                //{
                //    scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_SiteSecurity_Context_web_is_subweb__skipping_site_security_provisioning);
                //    return parser;
                //}

                if (web.IsSubSite() && template.Security.BreakRoleInheritance)
                {
                    web.BreakRoleInheritance(template.Security.CopyRoleAssignments, template.Security.ClearSubscopes);
                    web.Update();
                    web.Context.Load(web, w => w.HasUniqueRoleAssignments);
                    web.Context.ExecuteQueryRetry();
                }

                var siteSecurity = template.Security;

                if (web.EnsureProperty(w => w.HasUniqueRoleAssignments) && !web.IsNoScriptSite())
                {
                    string parsedAssociatedOwnerGroupName = parser.ParseString(template.Security.AssociatedOwnerGroup);
                    string parsedAssociatedMemberGroupName = parser.ParseString(template.Security.AssociatedMemberGroup);
                    string parsedAssociatedVisitorGroupName = parser.ParseString(template.Security.AssociatedVisitorGroup);

                    bool webNeedsUpdate = false;

                    if (parsedAssociatedOwnerGroupName != null)
                    {
                        if (string.IsNullOrEmpty(parsedAssociatedOwnerGroupName))
                        {
                            web.AssociatedOwnerGroup = null;
                            webNeedsUpdate = true;
                        }
                        else
                        {
                            if (!web.GroupExists(parsedAssociatedOwnerGroupName))
                            {
                                web.AssociatedOwnerGroup = EnsureGroup(web, parsedAssociatedOwnerGroupName);
                                webNeedsUpdate = true;
                            }
                            else if (!web.AssociatedOwnerGroup.ServerObjectIsNull.Value)
                            {
                                if (web.AssociatedOwnerGroup.Title != parsedAssociatedOwnerGroupName)
                                {
                                    var updatedOwnerGroup = web.SiteGroups.GetByName(parsedAssociatedOwnerGroupName);
                                    web.Context.Load(updatedOwnerGroup);
                                    web.Context.ExecuteQueryRetry();

                                    web.AssociatedOwnerGroup = updatedOwnerGroup;
                                    webNeedsUpdate = true;
                                }
                            }
                        }
                        if (webNeedsUpdate)
                        {
                            // Trigger the creation and setting of the associated owner group
                            web.Update();
                            web.Context.ExecuteQueryRetry();
                        }
                    }

                    if (parsedAssociatedMemberGroupName != null)
                    {
                        if (string.IsNullOrEmpty(parsedAssociatedMemberGroupName))
                        {
                            web.AssociatedMemberGroup = null;
                            webNeedsUpdate = true;
                        }
                        else
                        {
                            if (!web.GroupExists(parsedAssociatedMemberGroupName))
                            {
                                web.AssociatedMemberGroup = EnsureGroup(web, parsedAssociatedMemberGroupName);
                                webNeedsUpdate = true;
                            }
                            else if (!web.AssociatedMemberGroup.ServerObjectIsNull.Value)
                            {
                                if (web.AssociatedMemberGroup.Title != parsedAssociatedMemberGroupName)
                                {
                                    var updatedMemberGroup = web.SiteGroups.GetByName(parsedAssociatedMemberGroupName);
                                    web.Context.Load(updatedMemberGroup);
                                    web.Context.ExecuteQueryRetry();

                                    web.AssociatedMemberGroup = updatedMemberGroup;
                                    webNeedsUpdate = true;
                                }
                            }
                        }
                        if (webNeedsUpdate)
                        {
                            // Trigger the creation and setting of the associated member group
                            web.Update();
                            web.Context.ExecuteQueryRetry();
                        }
                    }

                    if (parsedAssociatedVisitorGroupName != null)
                    {
                        if (string.IsNullOrEmpty(parsedAssociatedVisitorGroupName))
                        {
                            web.AssociatedVisitorGroup = null;
                            webNeedsUpdate = true;
                        }
                        else
                        {
                            if (!web.GroupExists(parsedAssociatedVisitorGroupName))
                            {
                                web.AssociatedVisitorGroup = EnsureGroup(web, parsedAssociatedVisitorGroupName);
                                webNeedsUpdate = true;
                            }
                            else if (!web.AssociatedVisitorGroup.ServerObjectIsNull.Value)
                            {
                                if (web.AssociatedVisitorGroup.Title != parsedAssociatedVisitorGroupName)
                                {
                                    var updatedVisitorGroup = web.SiteGroups.GetByName(parsedAssociatedVisitorGroupName);
                                    web.Context.Load(updatedVisitorGroup);
                                    web.Context.ExecuteQueryRetry();

                                    web.AssociatedVisitorGroup = updatedVisitorGroup;
                                    webNeedsUpdate = true;
                                }
                            }
                        }
                        if (webNeedsUpdate)
                        {
                            // Trigger the creation and setting of the associated visitor group
                            web.Update();
                            web.Context.ExecuteQueryRetry();
                        }
                    }
                }

                var ownerGroup = web.AssociatedOwnerGroup;
                var memberGroup = web.AssociatedMemberGroup;
                var visitorGroup = web.AssociatedVisitorGroup;

                // need to load the groups for the ServerObjectIsNull()-check to get correct results
                web.Context.Load(ownerGroup);
                web.Context.Load(memberGroup);
                web.Context.Load(visitorGroup);
                web.Context.ExecuteQueryRetry();

                if (!ownerGroup.ServerObjectIsNull())
                {
                    web.Context.Load(ownerGroup, o => o.Title, o => o.Users);
                }
                if (!memberGroup.ServerObjectIsNull())<commit_msg>Restricting change to SharePoint Online<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Framework.Provisioning.Model;
using User = OfficeDevPnP.Core.Framework.Provisioning.Model.User;
using OfficeDevPnP.Core.Diagnostics;
using Microsoft.SharePoint.Client.Utilities;
using OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers.TokenDefinitions;
using RoleDefinition = Microsoft.SharePoint.Client.RoleDefinition;
using OfficeDevPnP.Core.Utilities;
using OfficeDevPnP.Core.Extensions;

namespace OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers
{
    internal class ObjectSiteSecurity : ObjectHandlerBase
    {
        public override string Name
        {
            get { return "Site Security"; }
        }

        public override string InternalName => "SiteSecurity";

        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                // Changed by <NAME> to embrace the new sub-site attributes to break role inheritance and copy role assignments
                // if this is a sub site then we're not provisioning security as by default security is inherited from the root site
                //if (web.IsSubSite() && !template.Security.BreakRoleInheritance)
                //{
                //    scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_SiteSecurity_Context_web_is_subweb__skipping_site_security_provisioning);
                //    return parser;
                //}

                if (web.IsSubSite() && template.Security.BreakRoleInheritance)
                {
                    web.BreakRoleInheritance(template.Security.CopyRoleAssignments, template.Security.ClearSubscopes);
                    web.Update();
                    web.Context.Load(web, w => w.HasUniqueRoleAssignments);
                    web.Context.ExecuteQueryRetry();
                }

                var siteSecurity = template.Security;

                if (web.EnsureProperty(w => w.HasUniqueRoleAssignments) && !web.IsNoScriptSite())
                {
                    string parsedAssociatedOwnerGroupName = parser.ParseString(template.Security.AssociatedOwnerGroup);
                    string parsedAssociatedMemberGroupName = parser.ParseString(template.Security.AssociatedMemberGroup);
                    string parsedAssociatedVisitorGroupName = parser.ParseString(template.Security.AssociatedVisitorGroup);

                    bool webNeedsUpdate = false;

                    if (parsedAssociatedOwnerGroupName != null)
                    {
                        if (string.IsNullOrEmpty(parsedAssociatedOwnerGroupName))
                        {
                            web.AssociatedOwnerGroup = null;
                            webNeedsUpdate = true;
                        }
                        else
                        {
                            if (!web.GroupExists(parsedAssociatedOwnerGroupName))
                            {
                                web.AssociatedOwnerGroup = EnsureGroup(web, parsedAssociatedOwnerGroupName);
                                webNeedsUpdate = true;
                            }
                            else if (!web.AssociatedOwnerGroup.ServerObjectIsNull.Value)
                            {
                                if (web.AssociatedOwnerGroup.Title != parsedAssociatedOwnerGroupName)
                                {
                                    var updatedOwnerGroup = web.SiteGroups.GetByName(parsedAssociatedOwnerGroupName);
                                    web.Context.Load(updatedOwnerGroup);
                                    web.Context.ExecuteQueryRetry();

                                    web.AssociatedOwnerGroup = updatedOwnerGroup;
                                    webNeedsUpdate = true;
                                }
                            }
                        }
                        if (webNeedsUpdate)
                        {
                            // Trigger the creation and setting of the associated owner group
                            web.Update();
                            web.Context.ExecuteQueryRetry();
                        }
                    }

                    if (parsedAssociatedMemberGroupName != null)
                    {
                        if (string.IsNullOrEmpty(parsedAssociatedMemberGroupName))
                        {
                            web.AssociatedMemberGroup = null;
                            webNeedsUpdate = true;
                        }
                        else
                        {
                            if (!web.GroupExists(parsedAssociatedMemberGroupName))
                            {
                                web.AssociatedMemberGroup = EnsureGroup(web, parsedAssociatedMemberGroupName);
                                webNeedsUpdate = true;
                            }
                            else if (!web.AssociatedMemberGroup.ServerObjectIsNull.Value)
                            {
                                if (web.AssociatedMemberGroup.Title != parsedAssociatedMemberGroupName)
                                {
                                    var updatedMemberGroup = web.SiteGroups.GetByName(parsedAssociatedMemberGroupName);
                                    web.Context.Load(updatedMemberGroup);
                                    web.Context.ExecuteQueryRetry();

                                    web.AssociatedMemberGroup = updatedMemberGroup;
                                    webNeedsUpdate = true;
                                }
                            }
                        }
                        if (webNeedsUpdate)
                        {
                            // Trigger the creation and setting of the associated member group
                            web.Update();
                            web.Context.ExecuteQueryRetry();
                        }
                    }

                    if (parsedAssociatedVisitorGroupName != null)
                    {
                        if (string.IsNullOrEmpty(parsedAssociatedVisitorGroupName))
                        {
                            web.AssociatedVisitorGroup = null;
                            webNeedsUpdate = true;
                        }
                        else
                        {
                            if (!web.GroupExists(parsedAssociatedVisitorGroupName))
                            {
                                web.AssociatedVisitorGroup = EnsureGroup(web, parsedAssociatedVisitorGroupName);
                                webNeedsUpdate = true;
                            }
                            else if (!web.AssociatedVisitorGroup.ServerObjectIsNull.Value)
                            {
                                if (web.AssociatedVisitorGroup.Title != parsedAssociatedVisitorGroupName)
                                {
                                    var updatedVisitorGroup = web.SiteGroups.GetByName(parsedAssociatedVisitorGroupName);
                                    web.Context.Load(updatedVisitorGroup);
                                    web.Context.ExecuteQueryRetry();

                                    web.AssociatedVisitorGroup = updatedVisitorGroup;
                                    webNeedsUpdate = true;
                                }
                            }
                        }
                        if (webNeedsUpdate)
                        {
                            // Trigger the creation and setting of the associated visitor group
                            web.Update();
                            web.Context.ExecuteQueryRetry();
                        }
                    }
                }

                var ownerGroup = web.AssociatedOwnerGroup;
                var memberGroup = web.AssociatedMemberGroup;
                var visitorGroup = web.AssociatedVisitorGroup;

#if !ONPREMISES
                // need to load the groups for the ServerObjectIsNull()-check to get correct results
                web.Context.Load(ownerGroup);
                web.Context.Load(memberGroup);
                web.Context.Load(visitorGroup);
                web.Context.ExecuteQueryRetry();
#endif

                if (!ownerGroup.ServerObjectIsNull())
                {
                    web.Context.Load(ownerGroup, o => o.Title, o => o.Users);
                }
                if (!memberGroup.ServerObjectIsNull())
{'fraction_non_alphanumeric': 0.06000343859246948, 'fraction_numerical': 0.0, 'mean_word_length': 1.1437346437346438, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '833569', 'n_tokens_mistral': 3638, 'n_tokens_neox': 3471, 'n_words': 618}