<?php
// Group creation and management uses the Save method, which allows for creation or
// alteration of any object within SpringCM.
// Since this method takes a SCMBaseObject array, expecting that the actual objects
// passed will inherit SCMBaseObject,
// we have to define PHP classes for the base and derived objects and map them to types
// defined in the WSDL.
$classMap = array('SCMBaseObject'=> 'BaseObject',
'SCMGroup'=>'Group',
'SCMContactInfo'=>'ContactInfo',
'SCMUserAccount'=>'UserAccount'); // create the mapping
class BaseObject {
public $Id;
public $Name;
public $ObjectType;
}
class Group extends BaseObject {
public $GroupType;
public $Members;
public $IsPrivate = false;
}
class UserAccount extends BaseObject {
public $FirstName;
public $LastName;
public $Email;
public $Role;
public $Status;
}
// Authenticate
$springCM = new SoapClient('https://soapna11.springcm.com/atlas/webservices/v201305/springcmservice.asmx?wsdl'
, array('classmap'=>$classMap)); // pass the class mapping into the SoapClient
$authResult = $springCM->AuthenticateNonDefault( array(
'userName'=>'<your id>',
'password'=>'<your password>',
'AccountId'=>'<your account id>',
'apiKey'=>'<your api key>'
));
// Get the inner object from the SOAP response
$token = $authResult->AuthenticateNonDefaultResult;
$groupName = '<name of the group to update>';
$userEmail = '<email of the user to add>';
// Find the group. This will find any group with a name containing the search string.
$groupResult = $springCM->GroupFind( array(
'token'=>$token,
'name'=>$groupName,
'IncludeDeleted'=>false
));
$groupResult = $groupResult->GroupFindResult;
// Determine whether to add the user to an existing group, create a new one, or do
// nothing (if the user is already in the group)
$userInGroup = false;
if (property_exists($groupResult, 'SCMGroup'))
{
$groupObject = $groupResult->SCMGroup;
// If the group has no members, create an array to hold
// the new member we'll add later
if (!property_exists($groupObject->Members, 'SCMContactInfo'))
{
$groupObject->Members->SCMContactInfo = array();
}
// If the group has exactly one member, the Members object does not contain an
// array but a single SCMUserAccount
// But when we call back to SpringCM, we have to pass an array
if (sizeof($groupObject->Members->SCMContactInfo) == 1)
{
$groupObject->Members->SCMContactInfo = array($groupObject->Members->SCMContactInfo);
}
// If the group has members, check to see if the member to be added is already
// there and set a flag if so
if (sizeof($groupObject->Members->SCMContactInfo) > 0)
{
foreach ($groupObject->Members->SCMContactInfo as $userAccount)
{
if ($userAccount->Email == $userEmail)
{
$userInGroup = true;
break;
}
}
unset($userAccount);
}
}
else
{
// No group found by the name requested; add new group
$groupObject = new Group();
$groupObject->Name = $groupName;
$groupObject->ObjectType = 'Group';
// Could also be 'Distribution' if you want a distribution group
$groupObject->GroupType = 'Security';
$groupObject->Members = new stdClass();
$groupObject->Members->SCMContactInfo = array();
}
if (!$userInGroup)
{
// Get additional information about the user to be added
$userObject = $springCM->ContactFind( array(
'token'=>$token,
'SearchText'=>$userEmail,
'IncludeDeleted'=>false
));
// Have to copy the info from ContactFind into
// a new object of the type defined above.
$userObject = $userObject->ContactFindResult->SCMContactInfo;
$userToAdd = new UserAccount();
foreach($userObject as $property => $value)
{
$userToAdd->$property = $value;
}
// Add the new member to the array
$groupObject->Members->SCMContactInfo[] = $userToAdd;
// Call back to SpringCM with the updated SCMGroup
$springCM->Save( array(
'token'=>$token,
'ToSave'=>array($groupObject),
'CreateIfNew'=>true
));
}
?>