Location: PHPKode > projects > iF.SVNAdmin > svnadmin/classes/providers/AuthFileGroupAndPathsProvider.class.php
<?php
/**
 * iF.SVNAdmin
 * Copyright (c) 2010 by Manuel Freiholz
 * http://www.insanefactory.com/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.
 */
namespace svnadmin\providers
{
  class AuthFileGroupAndPathProvider implements \svnadmin\core\interfaces\IGroupViewProvider,
                                                \svnadmin\core\interfaces\IGroupEditProvider,
                                                \svnadmin\core\interfaces\IPathsViewProvider,
                                                \svnadmin\core\interfaces\IPathsEditProvider
  {
    private $m_authfile = NULL;
    private $m_init_done = false;
    private static $m_instance = NULL;
    
    public static function getInstance()
    {
      if( self::$m_instance == NULL )
      {
        self::$m_instance = new AuthFileGroupAndPathProvider;
      }
      return self::$m_instance;
    }
    
    //////////////////////////////////////////////////////////////////////////////
    // -- Base interface implementations ----------------------------------------
    //////////////////////////////////////////////////////////////////////////////

    public function init()
    {
      global $appEngine;
      if( !$this->m_init_done )
      {
        $this->m_init_done = true;
        $this->m_authfile = new \IF_SVNAuthFileC;
        return $this->m_authfile->open($appEngine->getConfig()->getValue("Subversion", "SVNAuthFile"));
      }
      return true;
    }
    
    public function save()
    {
      return $this->m_authfile->save();
    }
    
    public function isUpdateable()
    {
      return false;
    }
    
    public function update()
    {
      return true;
    }

    //////////////////////////////////////////////////////////////////////////////
    // -- IGroupViewProvider -----------------------------------------------------
    //////////////////////////////////////////////////////////////////////////////

    public function getGroups()
    {
      $groupNamesArray = $this->m_authfile->groups();
      $retArray = array();
      if( is_array( $groupNamesArray ) )
      {
        for( $i=0; $i<count($groupNamesArray); $i++ )
        {
          $groupObj = new \svnadmin\core\entities\Group;
          $groupObj->id = $groupNamesArray[$i];
          $groupObj->name = $groupNamesArray[$i];
          array_push( $retArray, $groupObj );
        }
      }
      return $retArray;
    }

    public function groupExists( $objGroup )
    {
      return $this->m_authfile->groupExists( $objGroup->name );
    }

    public function getGroupsOfUser( $objUser )
    {
      $retArray = array();
      $groupNamesArray = $this->m_authfile->groupsOfUser( $objUser->name );
      if( is_array($groupNamesArray) )
      {
        for( $i=0; $i<count($groupNamesArray); $i++ )
        {
          $groupObj = new \svnadmin\core\entities\Group;
          $groupObj->id = $groupNamesArray[$i];
          $groupObj->name = $groupNamesArray[$i];
          array_push( $retArray, $groupObj );
        }
      }
      return $retArray;
    }

    public function getUsersOfGroup( $objGroup )
    {
      $retArray = array();
      $userNamesArray = $this->m_authfile->usersOfGroup( $objGroup->name );
      if( is_array($userNamesArray) )
      {
        for( $i=0; $i<count($userNamesArray); $i++ )
        {
          $userObj = new \svnadmin\core\entities\User();
          $userObj->id = $userNamesArray[$i];
          $userObj->name = $userNamesArray[$i];
          array_push( $retArray, $userObj );
        }
      }
      return $retArray;
    }
    
    public function isUserInGroup( $objUser, $objGroup )
    {
      return $this->m_authfile->isUserInGroup( $objGroup->name, $objUser->name );
    }

    //////////////////////////////////////////////////////////////////////////////
    // -- IGroupEditProvider -----------------------------------------------------
    //////////////////////////////////////////////////////////////////////////////

    public function addGroup( $objGroup )
    {
      return $this->m_authfile->createGroup($objGroup->name);
    }

    public function deleteGroup( $objGroup )
    {
      return $this->m_authfile->deleteGroup($objGroup->name);
    }
    
    public function assignUserToGroup( $objUser, $objGroup )
    {
      return $this->m_authfile->addUserToGroup( $objGroup->name, $objUser->name );
    }
    
    public function removeUserFromGroup( $objUser, $objGroup )
    {
      return $this->m_authfile->removeUserFromGroup( $objUser->name, $objGroup->name );
    }
    
    public function removeUserFromAllGroups( $objUser )
    {
      $groups = $this->m_authfile->groupsOfUser($objUser->name);
      for( $i=0; $i<count($groups); $i++ )
      {
        $this->m_authfile->removeUserFromGroup( $objUser->name, $groups[$i] );
      }
      return true;
    }
    
    //////////////////////////////////////////////////////////////////////////////
    // -- IPathsViewProvider -----------------------------------------------------
    //////////////////////////////////////////////////////////////////////////////
    
    public function getPaths()
    {
      $list = array();
      $paths = $this->m_authfile->repositories();
      for( $i=0; $i<count($paths); $i++ )
      {
        $o = new \svnadmin\core\entities\AccessPath;
        $o->path = $paths[$i];
        array_push( $list, $o );
      }
      return $list;
    }
    
    public function getPathsOfGroup( $objGroup )
    {
      $list = array();
      $perms = $this->m_authfile->permissionsOfGroup( $objGroup->name );
      $permsLen = count($perms);
      for( $i=0; $i<$permsLen; $i++ )
      {
        $oAP = new \svnadmin\core\entities\AccessPath;
        $oAP->path = $perms[$i][0];
        $oAP->perm = self::resolvePermission2($perms[$i][1]);
        array_push( $list, $oAP );
      }
      return $list;
    }
    
    public function getPathsOfUser( $objUser )
    {
      $list = array();
      $perms = $this->m_authfile->permissionsOfUser( $objUser->name );
      $permsLen = count($perms);
      for( $i=0; $i<$permsLen; $i++ )
      {
        $oAP = new \svnadmin\core\entities\AccessPath;
        $oAP->path = $perms[$i][0];
        $oAP->perm = self::resolvePermission2($perms[$i][1]);
        $oAP->inherited = $perms[$i][2];
        array_push( $list, $oAP );
      }
      return $list;
    }
    
    public function getUsersOfPath( $objAccessPath )
    {
      $users = $this->m_authfile->usersOfRepository( $objAccessPath->path );
      $list = array();
      
      for( $i=0; $i<count($users); $i++ )
      {
        $o = new \svnadmin\core\entities\User;
        $o->id = $users[$i][0];
        $o->name = $users[$i][0];
        $o->perm = self::resolvePermission2( $users[$i][1] );
        array_push( $list, $o );
      }

      return $list;
    }
    
    public function getGroupsOfPath( $objAccessPath )
    {
      $groups = $this->m_authfile->groupsOfRepository( $objAccessPath->path );
      $list = array();

      for( $i=0; $i<count($groups); $i++ )
      {
        $o = new \svnadmin\core\entities\Group;
        $o->id = $groups[$i][0];
        $o->name = $groups[$i][0];
        $o->perm = self::resolvePermission2( $groups[$i][1] );
        array_push( $list, $o );
      }

      return $list;
    }
    
    //////////////////////////////////////////////////////////////////////////////
    // -- IPathsEditProvider -----------------------------------------------------
    //////////////////////////////////////////////////////////////////////////////
    
    public function deleteAccessPath($objAccessPath)
    {
      return $this->m_authfile->removeRepositoryPath( $objAccessPath->path );
    }
    
    public function createAccessPath($objAccessPath)
    {
      return $this->m_authfile->addRepositoryPath( $objAccessPath->path );
    }
    
    public function removeGroupFromAllAccessPaths($objGroup)
    {
      $paths = $this->m_authfile->repositoryPathsOfGroup( $objGroup->name );
      for( $i=0; $i<count($paths); $i++ )
      {
        $this->m_authfile->removeGroupFromRepository( $objGroup->name, $paths[$i] );
      }
      return true;
    }
    
    public function removeUserFromAllAccessPaths($objUser)
    {
      $paths = $this->m_authfile->repositoryPathsOfUser( $objUser->name );
      for( $i=0; $i<count($paths); $i++ )
      {
        $this->m_authfile->removeUserFromRepository( $objUser->name, $paths[$i] );
      }
      return true;
    }
    
    public function removeUserFromAccessPath($objUser, $objAccessPath)
    {
      return $this->m_authfile->removeUserFromRepository( $objUser->name, $objAccessPath->path );
    }
    
    public function removeGroupFromAccessPath($objGroup, $objAccessPath)
    {
      return $this->m_authfile->removeGroupFromRepository( $objGroup->name, $objAccessPath->path );
    }
    
    public function assignGroupToAccessPath($objGroup, $objAccessPath, $objPermission)
    {
      $p = self::resolvePermission( $objPermission );
      if( $p !== FALSE )
      {
        return $this->m_authfile->addGroupToRepository( $objGroup->name, $objAccessPath->path, $p );
      }
      return false;
    }
    
    public function assignUserToAccessPath($objUser, $objAccessPath, $objPermission)
    {
      $p = self::resolvePermission( $objPermission );
      if( $p !== FALSE )
      {
        return $this->m_authfile->addUserToRepository( $objUser->name, $objAccessPath->path, $p );
      }
      return false;
    }
    
    private function resolvePermission( $objPermission )
    {
      $p = $objPermission->getPerm();
      if( $p == \svnadmin\core\entities\Permission::$PERM_NONE )
      {
        return \IF_SVNAuthFileC::$PERMISSION_NONE;
      }
      else if( $p == \svnadmin\core\entities\Permission::$PERM_READ )
      {
        return \IF_SVNAuthFileC::$PERMISSION_READ;
      }
      else if( $p == \svnadmin\core\entities\Permission::$PERM_READWRITE )
      {
        return \IF_SVNAuthFileC::$PERMISSION_READWRITE;
      }
      return \IF_SVNAuthFileC::$PERMISSION_NONE;
    }
    
    private function resolvePermission2( $strPerm )
    {
      if( $strPerm == \IF_SVNAuthFileC::$PERMISSION_NONE )
      {
        return \svnadmin\core\entities\Permission::$PERM_NONE;
      }
      else if( $strPerm == \IF_SVNAuthFileC::$PERMISSION_READ )
      {
        return \svnadmin\core\entities\Permission::$PERM_READ;
      }
      else if( $strPerm == \IF_SVNAuthFileC::$PERMISSION_READWRITE )
      {
        return \svnadmin\core\entities\Permission::$PERM_READWRITE;
      }
      return \svnadmin\core\entities\Permission::$PERM_NONE;
    }
  }
}
?>
Return current item: iF.SVNAdmin