예제 #1
0
        protected override void ExecuteCmdlet()
        {
            var list = Identity.GetList(CurrentWeb);

            if (list != null)
            {
                Principal principal = null;
                if (ParameterSetName == "Group")
                {
                    if (Group.Id != -1)
                    {
                        principal = CurrentWeb.SiteGroups.GetById(Group.Id);
                    }
                    else if (!string.IsNullOrEmpty(Group.Name))
                    {
                        principal = CurrentWeb.SiteGroups.GetByName(Group.Name);
                    }
                    else if (Group.Group != null)
                    {
                        principal = Group.Group;
                    }
                }
                else
                {
                    principal = CurrentWeb.EnsureUser(User);
                    ClientContext.ExecuteQueryRetry();
                }
                if (principal != null)
                {
                    if (!string.IsNullOrEmpty(AddRole))
                    {
                        var roleDefinition         = CurrentWeb.RoleDefinitions.GetByName(AddRole);
                        var roleDefinitionBindings = new RoleDefinitionBindingCollection(ClientContext);
                        roleDefinitionBindings.Add(roleDefinition);
                        var roleAssignments = list.RoleAssignments;
                        roleAssignments.Add(principal, roleDefinitionBindings);
                        ClientContext.Load(roleAssignments);
                        ClientContext.ExecuteQueryRetry();
                    }
                    if (!string.IsNullOrEmpty(RemoveRole))
                    {
                        var roleAssignment         = list.RoleAssignments.GetByPrincipal(principal);
                        var roleDefinitionBindings = roleAssignment.RoleDefinitionBindings;
                        ClientContext.Load(roleDefinitionBindings);
                        ClientContext.ExecuteQueryRetry();
                        foreach (var roleDefinition in roleDefinitionBindings.Where(roleDefinition => roleDefinition.Name == RemoveRole))
                        {
                            roleDefinitionBindings.Remove(roleDefinition);
                            roleAssignment.Update();
                            ClientContext.ExecuteQueryRetry();
                            break;
                        }
                    }
                }
                else
                {
                    WriteError(new ErrorRecord(new Exception("Principal not found"), "1", ErrorCategory.ObjectNotFound, null));
                }
            }
        }
예제 #2
0
        protected override void ExecuteCmdlet()
        {
            var user = CurrentWeb.EnsureUser(LoginName);

            ClientContext.Load(user, u => u.Email, u => u.Id, u => u.IsSiteAdmin, u => u.Groups, u => u.PrincipalType, u => u.Title, u => u.IsHiddenInUI, u => u.UserId, u => u.LoginName);
            ClientContext.ExecuteQueryRetry();
            WriteObject(user);
        }
예제 #3
0
        protected override void ExecuteCmdlet()
        {
            List list = null;

            if (List != null)
            {
                list = List.GetList(CurrentWeb);
            }
            if (list != null)
            {
                var item = Identity.GetListItem(list);
                if (item != null)
                {
                    item.EnsureProperties(i => i.HasUniqueRoleAssignments);
                    if (item.HasUniqueRoleAssignments && InheritPermissions.IsPresent)
                    {
                        item.ResetRoleInheritance();
                    }
                    else if (!item.HasUniqueRoleAssignments)
                    {
                        item.BreakRoleInheritance(!ClearExisting.IsPresent, true);
                    }
                    else if (ClearExisting.IsPresent)
                    {
                        item.ResetRoleInheritance();
                        item.BreakRoleInheritance(!ClearExisting.IsPresent, true);
                    }

                    if (SystemUpdate.IsPresent)
                    {
                        item.SystemUpdate();
                    }
                    else
                    {
                        item.Update();
                    }

                    ClientContext.ExecuteQueryRetry();

                    if (ParameterSetName == "Inherit")
                    {
                        // no processing of user/group needed
                        return;
                    }

                    Principal principal = null;
                    if (ParameterSetName == "Group")
                    {
                        if (Group.Id != -1)
                        {
                            principal = CurrentWeb.SiteGroups.GetById(Group.Id);
                        }
                        else if (!string.IsNullOrEmpty(Group.Name))
                        {
                            principal = CurrentWeb.SiteGroups.GetByName(Group.Name);
                        }
                        else if (Group.Group != null)
                        {
                            principal = Group.Group;
                        }
                    }
                    else
                    {
                        principal = CurrentWeb.EnsureUser(User);
                        ClientContext.ExecuteQueryRetry();
                    }
                    if (principal != null)
                    {
                        if (!string.IsNullOrEmpty(AddRole))
                        {
                            var roleDefinition         = CurrentWeb.RoleDefinitions.GetByName(AddRole);
                            var roleDefinitionBindings = new RoleDefinitionBindingCollection(ClientContext)
                            {
                                roleDefinition
                            };
                            var roleAssignments = item.RoleAssignments;
                            roleAssignments.Add(principal, roleDefinitionBindings);
                            ClientContext.Load(roleAssignments);
                            ClientContext.ExecuteQueryRetry();
                        }
                        if (!string.IsNullOrEmpty(RemoveRole))
                        {
                            var roleAssignment         = item.RoleAssignments.GetByPrincipal(principal);
                            var roleDefinitionBindings = roleAssignment.RoleDefinitionBindings;
                            ClientContext.Load(roleDefinitionBindings);
                            ClientContext.ExecuteQueryRetry();
                            foreach (var roleDefinition in roleDefinitionBindings.Where(roleDefinition => roleDefinition.Name == RemoveRole))
                            {
                                roleDefinitionBindings.Remove(roleDefinition);
                                roleAssignment.Update();
                                ClientContext.ExecuteQueryRetry();
                                break;
                            }
                        }
                    }
                    else
                    {
                        WriteError(new ErrorRecord(new Exception("Principal not found"), "1", ErrorCategory.ObjectNotFound, null));
                    }
                }
            }
        }
예제 #4
0
        protected override void ExecuteCmdlet()
        {
            var group = Identity.GetGroup(CurrentWeb);

            ClientContext.Load(group,
                               g => g.AllowMembersEditMembership,
                               g => g.AllowRequestToJoinLeave,
                               g => g.AutoAcceptRequestToJoinLeave,
                               g => g.OnlyAllowMembersViewMembership,
                               g => g.RequestToJoinLeaveEmailSetting);
            ClientContext.ExecuteQueryRetry();

            if (SetAssociatedGroup != AssociatedGroupType.None)
            {
                switch (SetAssociatedGroup)
                {
                case AssociatedGroupType.Visitors:
                {
                    CurrentWeb.AssociateDefaultGroups(null, null, group);
                    break;
                }

                case AssociatedGroupType.Members:
                {
                    CurrentWeb.AssociateDefaultGroups(null, group, null);
                    break;
                }

                case AssociatedGroupType.Owners:
                {
                    CurrentWeb.AssociateDefaultGroups(group, null, null);
                    break;
                }
                }
            }
            if (!string.IsNullOrEmpty(AddRole))
            {
                var roleDefinition         = CurrentWeb.RoleDefinitions.GetByName(AddRole);
                var roleDefinitionBindings = new RoleDefinitionBindingCollection(ClientContext);
                roleDefinitionBindings.Add(roleDefinition);
                var roleAssignments = CurrentWeb.RoleAssignments;
                roleAssignments.Add(group, roleDefinitionBindings);
                ClientContext.Load(roleAssignments);
                ClientContext.ExecuteQueryRetry();
            }
            if (!string.IsNullOrEmpty(RemoveRole))
            {
                var roleAssignment         = CurrentWeb.RoleAssignments.GetByPrincipal(group);
                var roleDefinitionBindings = roleAssignment.RoleDefinitionBindings;
                ClientContext.Load(roleDefinitionBindings);
                ClientContext.ExecuteQueryRetry();
                foreach (var roleDefinition in roleDefinitionBindings.Where(roleDefinition => roleDefinition.Name == RemoveRole))
                {
                    roleDefinitionBindings.Remove(roleDefinition);
                    roleAssignment.Update();
                    ClientContext.ExecuteQueryRetry();
                    break;
                }
            }

            var dirty = false;

            if (!string.IsNullOrEmpty(Title))
            {
                group.Title = Title;
                dirty       = true;
            }
            if (!string.IsNullOrEmpty(Description))
            {
                var groupItem = CurrentWeb.SiteUserInfoList.GetItemById(group.Id);
                CurrentWeb.Context.Load(groupItem, g => g["Notes"]);
                CurrentWeb.Context.ExecuteQueryRetry();

                var groupDescription = groupItem["Notes"]?.ToString();

                if (groupDescription != Description)
                {
                    groupItem["Notes"] = Description;
                    groupItem.Update();
                    dirty = true;
                }

                var plainTextDescription = PnP.Framework.Utilities.PnPHttpUtility.ConvertSimpleHtmlToText(Description, int.MaxValue);
                if (group.Description != plainTextDescription)
                {
                    //If the description is more than 512 characters long a server exception will be thrown.
                    group.Description = plainTextDescription;
                    dirty             = true;
                }
            }
            if (ParameterSpecified(nameof(AllowRequestToJoinLeave)) && AllowRequestToJoinLeave != group.AllowRequestToJoinLeave)
            {
                group.AllowRequestToJoinLeave = AllowRequestToJoinLeave;
                dirty = true;
            }

            if (ParameterSpecified(nameof(AutoAcceptRequestToJoinLeave)) && AutoAcceptRequestToJoinLeave != group.AutoAcceptRequestToJoinLeave)
            {
                group.AutoAcceptRequestToJoinLeave = AutoAcceptRequestToJoinLeave;
                dirty = true;
            }
            if (ParameterSpecified(nameof(AllowMembersEditMembership)) && AllowMembersEditMembership != group.AllowMembersEditMembership)
            {
                group.AllowMembersEditMembership = AllowMembersEditMembership;
                dirty = true;
            }
            if (ParameterSpecified(nameof(OnlyAllowMembersViewMembership)) && OnlyAllowMembersViewMembership != group.OnlyAllowMembersViewMembership)
            {
                group.OnlyAllowMembersViewMembership = OnlyAllowMembersViewMembership;
                dirty = true;
            }
            if (RequestToJoinEmail != group.RequestToJoinLeaveEmailSetting)
            {
                group.RequestToJoinLeaveEmailSetting = RequestToJoinEmail;
                dirty = true;
            }
            if (dirty)
            {
                group.Update();
                ClientContext.ExecuteQueryRetry();
            }


            if (!string.IsNullOrEmpty(Owner))
            {
                Principal groupOwner;

                try
                {
                    groupOwner  = CurrentWeb.EnsureUser(Owner);
                    group.Owner = groupOwner;
                    group.Update();
                    ClientContext.ExecuteQueryRetry();
                }
                catch
                {
                    groupOwner  = CurrentWeb.SiteGroups.GetByName(Owner);
                    group.Owner = groupOwner;
                    group.Update();
                    ClientContext.ExecuteQueryRetry();
                }
            }
        }
        protected override void ExecuteCmdlet()
        {
            // Retrieve the list
            List list = null;

            if (List != null)
            {
                list = List.GetList(CurrentWeb);
            }

            // Ensure the list exists
            if (list == null)
            {
                throw new PSArgumentException("The provided List through the List parameter could not be found", nameof(List));
            }

            // Retrieve the list item
            var item = Identity.GetListItem(list);

            // Ensure the list item exists
            if (item == null)
            {
                throw new PSArgumentException("The provided list item through the Identity parameter could not be found", nameof(Identity));
            }

            item.EnsureProperties(i => i.HasUniqueRoleAssignments);

            if (ParameterSetName == ParameterSet_INHERIT)
            {
                if (item.HasUniqueRoleAssignments && InheritPermissions.IsPresent)
                {
                    item.ResetRoleInheritance();
                    if (SystemUpdate)
                    {
                        item.SystemUpdate();
                    }
                    else
                    {
                        item.Update();
                    }
                    ClientContext.ExecuteQueryRetry();
                }
            }
            else
            {
                if (!item.HasUniqueRoleAssignments)
                {
                    item.BreakRoleInheritance(!ClearExisting.IsPresent, true);
                }
                else if (ClearExisting.IsPresent)
                {
                    item.ResetRoleInheritance();
                    item.BreakRoleInheritance(!ClearExisting.IsPresent, true);
                }

                if (SystemUpdate.IsPresent)
                {
                    item.SystemUpdate();
                }
                else
                {
                    item.Update();
                }

                ClientContext.ExecuteQueryRetry();

                Principal principal = null;
                if (ParameterSetName == ParameterSet_GROUP)
                {
                    if (Group.Id != -1)
                    {
                        principal = CurrentWeb.SiteGroups.GetById(Group.Id);
                    }
                    else if (!string.IsNullOrEmpty(Group.Name))
                    {
                        principal = CurrentWeb.SiteGroups.GetByName(Group.Name);
                    }
                    else if (Group.Group != null)
                    {
                        principal = Group.Group;
                    }
                }
                else
                {
                    principal = CurrentWeb.EnsureUser(User);
                    ClientContext.ExecuteQueryRetry();
                }
                if (principal == null)
                {
                    throw new PSArgumentException("The provided principal through the Principal parameter could not be found", nameof(Principal));
                }
                if (ParameterSpecified(nameof(AddRole)))
                {
                    var roleDefinition         = AddRole.GetRoleDefinition(ClientContext.Site);
                    var roleDefinitionBindings = new RoleDefinitionBindingCollection(ClientContext)
                    {
                        roleDefinition
                    };
                    var roleAssignments = item.RoleAssignments;
                    roleAssignments.Add(principal, roleDefinitionBindings);
                    ClientContext.Load(roleAssignments);
                    ClientContext.ExecuteQueryRetry();
                }
                if (ParameterSpecified(nameof(RemoveRole)))
                {
                    var roleDefinition         = RemoveRole.GetRoleDefinition(ClientContext.Site);
                    var roleAssignment         = item.RoleAssignments.GetByPrincipal(principal);
                    var roleDefinitionBindings = roleAssignment.RoleDefinitionBindings;
                    ClientContext.Load(roleDefinitionBindings);
                    ClientContext.ExecuteQueryRetry();
                    foreach (var roleDefinitionBinding in roleDefinitionBindings.Where(rd => rd.Name == roleDefinition.Name))
                    {
                        roleDefinitionBindings.Remove(roleDefinitionBinding);
                        roleAssignment.Update();
                        ClientContext.ExecuteQueryRetry();
                        break;
                    }
                }
            }
        }
        protected override void ExecuteCmdlet()
        {
            List list = null;

            if (List != null)
            {
                list = List.GetList(CurrentWeb);
            }
            if (list != null)
            {
                // Try to get an instance to the folder
                var folder = Identity.GetFolder(CurrentWeb);

                // Ensure the folder has been found
                if (folder == null)
                {
                    WriteError(new ErrorRecord(new Exception("Folder not found"), "1", ErrorCategory.ObjectNotFound, null));
                }

                // Ensure we have access to the ListItemAllFields property of the folder
                folder.EnsureProperty(f => f.ListItemAllFields);

                // Validate that the ListItemAllFields contains the Id which represents the ListItem ID equivallent for this folder
                if (folder.ListItemAllFields.Id <= 0)
                {
                    WriteError(new ErrorRecord(new Exception("ListItemId on folder not found"), "1", ErrorCategory.InvalidData, null));
                }

                // Get the list item which is the equivallent of the folder
                var item = list.GetItemById(folder.ListItemAllFields.Id);

                // Perform the permission operations on the listitem belonging to the folder
                item.EnsureProperties(i => i.HasUniqueRoleAssignments);
                if (item.HasUniqueRoleAssignments && InheritPermissions.IsPresent)
                {
                    item.ResetRoleInheritance();
                }
                else if (!item.HasUniqueRoleAssignments)
                {
                    item.BreakRoleInheritance(!ClearExisting.IsPresent, true);
                }
                else if (ClearExisting.IsPresent)
                {
                    item.ResetRoleInheritance();
                    item.BreakRoleInheritance(!ClearExisting.IsPresent, true);
                }

                if (SystemUpdate.IsPresent)
                {
                    item.SystemUpdate();
                }
                else
                {
                    item.Update();
                }
                ClientContext.ExecuteQueryRetry();
                if (ParameterSetName == "Inherit")
                {
                    // no processing of user/group needed
                    return;
                }

                Principal principal = null;
                if (ParameterSetName == "Group")
                {
                    if (Group.Id != -1)
                    {
                        principal = CurrentWeb.SiteGroups.GetById(Group.Id);
                    }
                    else if (!string.IsNullOrEmpty(Group.Name))
                    {
                        principal = CurrentWeb.SiteGroups.GetByName(Group.Name);
                    }
                    else if (Group.Group != null)
                    {
                        principal = Group.Group;
                    }
                }
                else
                {
                    principal = CurrentWeb.EnsureUser(User);
                    ClientContext.ExecuteQueryRetry();
                }
                if (principal != null)
                {
                    if (!string.IsNullOrEmpty(AddRole))
                    {
                        var roleDefinition         = CurrentWeb.RoleDefinitions.GetByName(AddRole);
                        var roleDefinitionBindings = new RoleDefinitionBindingCollection(ClientContext)
                        {
                            roleDefinition
                        };
                        var roleAssignments = item.RoleAssignments;
                        roleAssignments.Add(principal, roleDefinitionBindings);
                        ClientContext.Load(roleAssignments);
                        ClientContext.ExecuteQueryRetry();
                    }
                    if (!string.IsNullOrEmpty(RemoveRole))
                    {
                        var roleAssignment         = item.RoleAssignments.GetByPrincipal(principal);
                        var roleDefinitionBindings = roleAssignment.RoleDefinitionBindings;
                        ClientContext.Load(roleDefinitionBindings);
                        ClientContext.ExecuteQueryRetry();
                        foreach (var roleDefinition in roleDefinitionBindings.Where(roleDefinition => roleDefinition.Name == RemoveRole))
                        {
                            roleDefinitionBindings.Remove(roleDefinition);
                            roleAssignment.Update();
                            ClientContext.ExecuteQueryRetry();
                            break;
                        }
                    }
                }
                else
                {
                    WriteError(new ErrorRecord(new Exception("Principal not found"), "1", ErrorCategory.ObjectNotFound, null));
                }
            }
        }