Exemple #1
0
        protected override void ExecuteCmdlet()
        {
            if (!ParameterSpecified(nameof(AddRole)) && !ParameterSpecified(nameof(RemoveRole)))
            {
                throw new PSInvalidOperationException("AddRole or RemoveRole is required");
            }

            Web web = CurrentWeb;

            if (ParameterSpecified(nameof(Identity)))
            {
                web = Identity.GetWeb(ClientContext);
            }

            Principal principal = null;

            if (ParameterSetName == ParameterSet_GROUP)
            {
                if (Group.Id != -1)
                {
                    principal = web.SiteGroups.GetById(Group.Id);
                }
                else if (!string.IsNullOrEmpty(Group.Name))
                {
                    principal = web.SiteGroups.GetByName(Group.Name);
                }
                else if (Group.Group != null)
                {
                    principal = Group.Group;
                }
            }
            else
            {
                principal = web.EnsureUser(User);
            }
            if (principal != null)
            {
                if (AddRole != null)
                {
                    foreach (var role in AddRole)
                    {
                        web.AddPermissionLevelToPrincipal(principal, role);
                    }
                }
                if (RemoveRole != null)
                {
                    foreach (var role in RemoveRole)
                    {
                        web.RemovePermissionLevelFromPrincipal(principal, role);
                    }
                }
            }
            else
            {
                WriteError(new ErrorRecord(new Exception("Principal not found"), "1", ErrorCategory.ObjectNotFound, null));
            }
        }
Exemple #2
0
        protected override void ExecuteCmdlet()
        {
            // Get Web
            Web web = SelectedWeb;

            if (ParameterSetName == "GroupByWebIdentity" || ParameterSetName == "UserByWebIdentity")
            {
                if (Identity.Id != Guid.Empty)
                {
                    web = ClientContext.Web.GetWebById(Identity.Id);
                }
                else if (Identity.Web != null)
                {
                    web = Identity.Web;
                }
                else if (Identity.Url != null)
                {
                    web = ClientContext.Web.GetWebByUrl(Identity.Url);
                }
            }
            else if (ParameterSetName == "GroupByWebUrl" || ParameterSetName == "UserByWebUrl")
            {
                web = SelectedWeb.GetWeb(Url);
            }

            // Set permissions
            Principal principal = null;

            if (ParameterSetName == "Group" || ParameterSetName == "GroupByWebUrl" || ParameterSetName == "GroupByWebIdentity")
            {
                if (Group.Id != -1)
                {
                    principal = web.SiteGroups.GetById(Group.Id);
                }
                else if (!string.IsNullOrEmpty(Group.Name))
                {
                    principal = web.SiteGroups.GetByName(Group.Name);
                }
                else if (Group.Group != null)
                {
                    principal = Group.Group;
                }
            }
            else
            {
                principal = web.EnsureUser(User);
            }
            if (principal != null)
            {
                if (AddRole != null)
                {
                    foreach (var role in AddRole)
                    {
                        web.AddPermissionLevelToPrincipal(principal, role);
                    }
                }
                if (RemoveRole != null)
                {
                    foreach (var role in RemoveRole)
                    {
                        web.RemovePermissionLevelFromPrincipal(principal, role);
                    }
                }
            }
            else
            {
                WriteError(new ErrorRecord(new Exception("Principal not found"), "1", ErrorCategory.ObjectNotFound, null));
            }
        }