diff --git a/src/SharePoint/RoleAssignment.php b/src/SharePoint/RoleAssignment.php index e942f77d..ef80afbb 100644 --- a/src/SharePoint/RoleAssignment.php +++ b/src/SharePoint/RoleAssignment.php @@ -53,5 +53,10 @@ function getProperty($name) return parent::getProperty($name); } + public function getPrincipalId() + { + return $this->getProperty("PrincipalId"); + } + -} \ No newline at end of file +} diff --git a/src/SharePoint/RoleAssignmentCollection.php b/src/SharePoint/RoleAssignmentCollection.php index a629a71c..7103c74f 100644 --- a/src/SharePoint/RoleAssignmentCollection.php +++ b/src/SharePoint/RoleAssignmentCollection.php @@ -7,9 +7,14 @@ use Office365\PHP\Client\Runtime\ClientObjectCollection; +use Office365\PHP\Client\Runtime\InvokeMethodQuery; use Office365\PHP\Client\Runtime\InvokePostMethodQuery; use Office365\PHP\Client\Runtime\ResourcePathEntity; +use Office365\PHP\Client\Runtime\ResourcePathServiceOperation; +/** + * Represents a collection of RoleAssignment objects that defines all the role assignments for each securable object. + */ class RoleAssignmentCollection extends ClientObjectCollection { /** @@ -26,8 +31,8 @@ public function getGroups() /** * Adds a role assignment to the collection of role assignment objects - * @param $principalId - * @param $roleDefId + * @param $principalId string The unique identifier of the role assignment. + * @param $roleDefId string */ public function addRoleAssignment($principalId,$roleDefId) { @@ -37,4 +42,33 @@ public function addRoleAssignment($principalId,$roleDefId) )); $this->getContext()->addQuery($qry); } + + /** + * Gets the role assignment associated with the specified principal ID from the collection. + * @param $principalId + * @return RoleAssignment + */ + public function getByPrincipalId($principalId) + { + $path = new ResourcePathServiceOperation($this->getContext(),$this->getResourcePath(),"getByPrincipalId",array( + $principalId + )); + $roleAssignment = new RoleAssignment($this->getContext(),$path); + $this->addChild($roleAssignment); + return $roleAssignment; + } + + /** + * Removes the role assignment with the specified principal and role definition from the collection. + * @param $principalId string The unique identifier of the role assignment. + * @param $roleDefId string + */ + public function removeRoleAssignment($principalId,$roleDefId) + { + $qry = new InvokePostMethodQuery($this->getResourcePath(), "removeroleassignment", array( + "principalid" => $principalId, + "roledefid" => $roleDefId + )); + $this->getContext()->addQuery($qry); + } } diff --git a/src/SharePoint/RoleDefinitionCollection.php b/src/SharePoint/RoleDefinitionCollection.php index de08d21b..c1edce2d 100644 --- a/src/SharePoint/RoleDefinitionCollection.php +++ b/src/SharePoint/RoleDefinitionCollection.php @@ -5,6 +5,9 @@ use Office365\PHP\Client\Runtime\ClientObjectCollection; +use Office365\PHP\Client\Runtime\InvokeMethodQuery; +use Office365\PHP\Client\Runtime\ResourcePathServiceOperation; + /** * Represents the collection of RoleDefinition objects that define the role definitions that are available for use within the Web site. @@ -12,6 +15,51 @@ class RoleDefinitionCollection extends ClientObjectCollection { + /** + * Gets the role definition with the specified ID from the collection. + * @param $Id string The ID of the role definition. + * @return RoleDefinition + */ + public function getById($Id) + { + $path = new ResourcePathServiceOperation($this->getContext(), $this->getResourcePath(), "getbyid", array( + $Id + )); + $roleDef = new RoleDefinition($this->getContext(), $path); + $this->addChild($roleDef); + return $roleDef; + } + + /** + * Gets the role definition with the specified name. + * @param $name string The name of the role definition. + * @return RoleDefinition + */ + public function getByName($name) + { + $path = new ResourcePathServiceOperation($this->getContext(),$this->getResourcePath(),"getbyname",array( + $name + )); + $roleDef = new RoleDefinition($this->getContext(),$path); + $this->addChild($roleDef); + return $roleDef; + } + + /** + * Gets the role definition with the specified role type. + * @param $type string he RoleTypeKind of the role definition. See RoleType object for a list of role type values. + * @return RoleDefinition + */ + public function getByType($type) + { + $qry = new InvokeMethodQuery($this->getResourcePath(), "getbytype", array( + $type + )); + $roleDef = new RoleDefinition($this->getContext(),$qry->getResourcePath()); + $this->getContext()->addQuery($qry,$roleDef); + $this->addChild($roleDef); + return $roleDef; + } } diff --git a/src/SharePoint/RoleDefinitionCreationInformation.php b/src/SharePoint/RoleDefinitionCreationInformation.php index 3348e747..19e285e8 100644 --- a/src/SharePoint/RoleDefinitionCreationInformation.php +++ b/src/SharePoint/RoleDefinitionCreationInformation.php @@ -10,4 +10,27 @@ class RoleDefinitionCreationInformation extends ClientValueObject { -} \ No newline at end of file + /** + * @var string Gets or sets a value that specifies the name of the role definition. + */ + public $Name; + + + /** + * @var string Gets or sets a value that specifies a description of the role definition. + */ + public $Description; + + + /** + * @var integer Gets or sets a value that specifies the order in which roles are displayed. + */ + public $Order; + + + /** + * @var BasePermissions Gets or sets a value that specifies the permissions for the role definition. + */ + public $BasePermissions; + +} diff --git a/src/SharePoint/RoleType.php b/src/SharePoint/RoleType.php index df24336b..433fe2a5 100644 --- a/src/SharePoint/RoleType.php +++ b/src/SharePoint/RoleType.php @@ -44,7 +44,21 @@ class RoleType */ const Contributor = 3; + /** + * Has Contributor rights, plus rights to cancel check-out, delete items, manage lists, add and customize pages, + * define and apply themes and borders, and link style sheets. Includes all rights in the Contributor role, + * plus the following: AddAndCustomizePages, ApplyStyleSheets, ApplyThemeAndBorder, CancelCheckout, ManageLists. + * WebDesigners can modify the structure of the site and create new lists or document libraries. + */ const WebDesigner = 4; + /** + * Has all rights from other roles, plus rights to manage roles and view usage analysis data. + * Includes all rights in the WebDesigner role, plus the following: + * ManageListPermissions, ManageRoles, ManageSubwebs, ViewUsageData. + * The Administrator role cannot be customized or deleted, and must always contain at least one member. + * Members of the Administrator role always have access to, or can grant themselves access to, + * any item in the Web site. + */ const Administrator = 5; } diff --git a/src/SharePoint/Web.php b/src/SharePoint/Web.php index 158086ba..d2336273 100644 --- a/src/SharePoint/Web.php +++ b/src/SharePoint/Web.php @@ -185,7 +185,7 @@ public function getSiteGroups() /** * Gets the collection of role definitions for the Web site. - * @return RoleAssignmentCollection + * @return RoleDefinitionCollection */ public function getRoleDefinitions() { diff --git a/tests/RoleTest.php b/tests/RoleTest.php new file mode 100644 index 00000000..4a36277f --- /dev/null +++ b/tests/RoleTest.php @@ -0,0 +1,99 @@ +getWeb(), $listTitle, ListTemplateType::DocumentLibrary); + } + + public static function tearDownAfterClass() + { + self::$securedTargetObject->deleteObject(); + self::$context->executeQuery(); + parent::tearDownAfterClass(); + } + + public function testSetUniquePerms() + { + self::$securedTargetObject->breakRoleInheritance(false); + self::$context->executeQuery(); + + self::$context->load(self::$securedTargetObject,["HasUniqueRoleAssignments"]); + self::$context->executeQuery(); + $value = self::$securedTargetObject->hasUniqueRoleAssignments(); + self::assertTrue($value); + } + + /** + * @depends testSetUniquePerms + */ + public function testGetRoleDefinition() + { + $roleName = "Edit"; + $roleDef = self::$context->getWeb()->getRoleDefinitions()->getByName($roleName); //get role definition by name + self::$context->load($roleDef); + self::$context->executeQuery(); + self::assertNotNull($roleDef); + self::assertEquals($roleDef->getProperty("Name"),$roleName); + return $roleDef; + } + + /** + * @depends testGetRoleDefinition + * @param RoleDefinition $targetRole + * @return RoleAssignment + */ + public function testAddRoleAssignment(RoleDefinition $targetRole){ + //get site user + $usersResult = self::$context->getWeb()->getSiteUsers()->filter("Title eq 'Marta Doe'"); + self::$context->load($usersResult); + self::$context->executeQuery(); + self::assertEquals(1,$usersResult->getCount()); + + self::$securedTargetObject->getRoleAssignments()->addRoleAssignment($usersResult->getItem(0)->getProperty("Id"),$targetRole->getProperty("Id")); + self::$context->executeQuery(); + + $roleAssignment = self::$securedTargetObject->getRoleAssignments()->getByPrincipalId($usersResult->getItem(0)->getProperty("Id")); + self::$context->load($roleAssignment); + self::$context->executeQuery(); + self::assertNotNull($roleAssignment); + return $roleAssignment; + } + + /** + * @depends testAddRoleAssignment + * @param RoleAssignment $roleAssignment + */ + public function testRemoveRoleAssignment(RoleAssignment $roleAssignment) + { + $roleDef = self::$context->getWeb()->getRoleDefinitions()->getByName("Edit"); //get role definition by name + self::$context->load($roleDef); + $roleAssignmentsBefore = self::$securedTargetObject->getRoleAssignments(); + self::$context->load($roleAssignmentsBefore); + self::$context->executeQuery(); + self::assertNotNull($roleDef); + + $rolesCount = $roleAssignmentsBefore->getCount(); + self::$securedTargetObject->getRoleAssignments()->removeRoleAssignment($roleAssignment->getPrincipalId(),$roleDef->getProperty("Id")); + self::$context->executeQuery(); + + $roleAssignmentsAfter = self::$securedTargetObject->getRoleAssignments(); + self::$context->load($roleAssignmentsAfter); + self::$context->executeQuery(); + self::assertEquals($roleAssignmentsAfter->getCount(),$rolesCount -1); + } + + +} diff --git a/tests/WebTest.php b/tests/WebTest.php index 69db8d00..bf425617 100644 --- a/tests/WebTest.php +++ b/tests/WebTest.php @@ -1,13 +1,9 @@ getData(), - function (\Office365\PHP\Client\SharePoint\RoleAssignment $assignment) { + function (RoleAssignment $assignment) { $principal = $assignment->getMember(); return ($principal->getProperty("PrincipalType") === PrincipalType::SharePointGroup || $principal->getProperty("PrincipalType") === PrincipalType::User); @@ -57,10 +53,10 @@ public function testCreateWeb() /** * @depends testCreateWeb - * @param \Office365\PHP\Client\SharePoint\Web $targetWeb - * @return \Office365\PHP\Client\SharePoint\Web + * @param Web $targetWeb + * @return Web */ - public function testIfWebExist(\Office365\PHP\Client\SharePoint\Web $targetWeb) + public function testIfWebExist(Web $targetWeb) { $webTitle = $targetWeb->getProperty('Title'); $webs = self::$context->getWeb()->getWebs()->filter("Title eq '$webTitle'"); @@ -73,10 +69,10 @@ public function testIfWebExist(\Office365\PHP\Client\SharePoint\Web $targetWeb) /** * @depends testCreateWeb - * @param \Office365\PHP\Client\SharePoint\Web $targetWeb - * @return \Office365\PHP\Client\SharePoint\Web + * @param Web $targetWeb + * @return Web */ - public function testUpdateWeb(\Office365\PHP\Client\SharePoint\Web $targetWeb) + public function testUpdateWeb(Web $targetWeb) { $ctx = $targetWeb->getContext(); $webTitle = ListItemExtensions::createUniqueName("WS_Updated"); @@ -110,9 +106,9 @@ public function testAssignUniquePermissions(Web $targetWeb){ /** * @depends testCreateWeb - * @param \Office365\PHP\Client\SharePoint\Web $targetWeb + * @param Web $targetWeb */ - public function testTryDeleteWeb(\Office365\PHP\Client\SharePoint\Web $targetWeb){ + public function testTryDeleteWeb(Web $targetWeb){ $title = $targetWeb->getProperty("Title"); $targetWeb->deleteObject(); self::$context->executeQuery();