public function load($filename)
 {
     $json = file_get_contents($filename);
     $data = json_decode($json, true);
     $policy = new Policy();
     if (isset($data['Version'])) {
         $policy->setVersion($data['Version']);
     }
     foreach ($data['Statement'] as $sData) {
         $statement = new Statement();
         if (isset($sData['Sid'])) {
             $statement->setId($sData['Sid']);
         }
         $statement->setEffect($sData['Effect']);
         foreach ($sData['Action'] as $aData) {
             $action = new Action($aData);
             $statement->addAction($action);
         }
         if (isset($sData['Resource'])) {
             foreach ($sData['Resource'] as $rData) {
                 $resource = new Resource($rData);
                 $statement->addResource($resource);
             }
         }
         if (isset($sData['Principal'])) {
             $pData = $sData['Principal'];
             foreach ($pData as $key => $value) {
                 $principal = new Principal($key, $value);
                 $statement->setPrincipal($principal);
             }
         }
         $policy->addStatement($statement);
     }
     return $policy;
 }
 public function serialize(Policy $policy)
 {
     $data = array();
     $data['Version'] = $policy->getVersion();
     $data['Statement'] = array();
     foreach ($policy->getStatements() as $statement) {
         $sData = array();
         if ($statement->getId()) {
             $sData['Sid'] = $statement->getId();
         }
         $sData['Effect'] = $statement->getEffect();
         $sData['Action'] = array();
         foreach ($statement->getActions() as $action) {
             $sData['Action'][] = (string) $action;
         }
         foreach ($statement->getResources() as $resource) {
             $sData['Resource'][] = (string) $resource;
         }
         $principal = $statement->getPrincipal();
         if ($principal) {
             $sData['Principal'] = array($principal->getType() => $principal->getKeys());
         }
         $data['Statement'][] = $sData;
     }
     return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
 }
Exemplo n.º 3
0
 public function testPolicies()
 {
     $policy = new Policy();
     $statement = new Statement();
     $action = new Action('s3', 'Get*');
     $statement->addAction($action);
     $action = new Action('s3', 'List*');
     $statement->addAction($action);
     $resource = new Resource('xrn:aws:s3:eu-west-1::some-bucket');
     $statement->addResource($resource);
     $principal = new Principal('AWS');
     $principal->addKey('xrn:aws:iam::AWS-account-ID:user/bob');
     $principal->addKey('xrn:aws:iam::AWS-account-ID:user/alice');
     $statement->setPrincipal($principal);
     $policy->addStatement($statement);
     $serializer = new JsonPolicySerializer();
     $json = $serializer->serialize($policy);
     //echo $json;
 }