Skip to content

Commit

Permalink
Introduced support for roles (RoleAssignment, RoleDefinition entities…
Browse files Browse the repository at this point in the history
… and etc) in SharePoint API
  • Loading branch information
vgrem committed Sep 19, 2019
1 parent 93c6418 commit c8444fa
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 19 deletions.
7 changes: 6 additions & 1 deletion src/SharePoint/RoleAssignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,10 @@ function getProperty($name)
return parent::getProperty($name);
}

public function getPrincipalId()
{
return $this->getProperty("PrincipalId");
}


}
}
38 changes: 36 additions & 2 deletions src/SharePoint/RoleAssignmentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand All @@ -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)
{
Expand All @@ -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);
}
}
48 changes: 48 additions & 0 deletions src/SharePoint/RoleDefinitionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,61 @@


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.
*/
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;
}

}
25 changes: 24 additions & 1 deletion src/SharePoint/RoleDefinitionCreationInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,27 @@
class RoleDefinitionCreationInformation extends ClientValueObject
{

}
/**
* @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;

}
14 changes: 14 additions & 0 deletions src/SharePoint/RoleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/SharePoint/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function getSiteGroups()

/**
* Gets the collection of role definitions for the Web site.
* @return RoleAssignmentCollection
* @return RoleDefinitionCollection
*/
public function getRoleDefinitions()
{
Expand Down
99 changes: 99 additions & 0 deletions tests/RoleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Office365\PHP\Client\SharePoint;


use ListExtensions;
use SharePointTestCase;

class RoleTest extends SharePointTestCase
{

private static $securedTargetObject;

public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
$listTitle = "Documents_" . rand(1, 100000);
self::$securedTargetObject = ListExtensions::ensureList(self::$context->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);
}


}
24 changes: 10 additions & 14 deletions tests/WebTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<?php

use Office365\PHP\Client\SharePoint\PrincipalType;
use Office365\PHP\Client\SharePoint\RoleAssignment;
use Office365\PHP\Client\SharePoint\Web;

require_once('SharePointTestCase.php');
require_once('ListItemExtensions.php');




class WebTest extends SharePointTestCase
{
Expand Down Expand Up @@ -38,7 +34,7 @@ public function testGetWebUsers()

$result = array_filter(
$assignments->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);
Expand All @@ -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'");
Expand All @@ -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");
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c8444fa

Please sign in to comment.