Turba::permissionsFilter PHP Method

permissionsFilter() public static method

Filters sources based on permissions.
public static permissionsFilter ( array $in, integer $permission = Horde_Perms::READ, array $options = [] ) : array
$in array The source list we want filtered.
$permission integer The Horde_Perms::* constant we will filter on.
$options array Additional options: - require_add: (boolean) Only return sources that can be added to.
return array The filtered data.
    public static function permissionsFilter(array $in, $permission = Horde_Perms::READ, array $options = array())
    {
        $factory = $GLOBALS['injector']->getInstance('Turba_Factory_Driver');
        $out = array();
        foreach ($in as $sourceId => $source) {
            if (!isset($source['type'])) {
                continue;
            }
            try {
                $driver = $factory->create($source, $sourceId);
                if ($driver->hasPermission($permission) && (empty($options['require_add']) || $driver->canAdd())) {
                    $out[$sourceId] = $source;
                }
            } catch (Turba_Exception $e) {
                Horde::log($e, 'ERR');
            }
        }
        return $out;
    }

Usage Example

Example #1
0
 /**
  */
 public function davGetCollections($user)
 {
     global $injector, $registry;
     $hordeUser = $registry->convertUsername($user, true);
     $dav = $injector->getInstance('Horde_Dav_Storage');
     $factory = $injector->getInstance('Turba_Shares');
     $books = array();
     foreach (Turba::getAddressBooks(Horde_Perms::SHOW) as $id => $book) {
         $readOnly = false;
         switch ($book['type']) {
             // Ugly hack! There is currently no clean way to retrieve address
             // books that the user "owns", or to find out if a SQL/LDAP/Kolab
             // address book contains per-user or global contacts.
             case 'share':
                 $share = $factory->getShare($id);
                 if ($user == '-system-' && strlen($share->get('owner')) || $user != '-system-' && $hordeUser != $share->get('owner') && $hordeUser != $registry->getAuth()) {
                     continue;
                 }
                 $readOnly = !$share->hasPermission($hordeUser, Horde_Perms::EDIT);
                 break;
             case 'facebook':
             case 'favourites':
             case 'vbook':
                 if ($user == '-system-') {
                     continue;
                 }
                 $readOnly = true;
                 break;
             default:
                 if (!Turba::permissionsFilter(array($id => $book), Horde_Perms::EDIT)) {
                     $readOnly = true;
                 }
                 break;
         }
         try {
             $id = $dav->getExternalCollectionId($id, 'contacts') ?: $id;
         } catch (Horde_Dav_Exception $e) {
         }
         $books[] = array('id' => $id, 'uri' => $id, 'principaluri' => 'principals/' . $user, '{DAV:}displayname' => $book['title'], '{' . CardDAV\Plugin::NS_CARDDAV . '}supported-address-data' => new CardDAV\Property\SupportedAddressData(array(array('contentType' => 'text/directory', 'version' => '3.0'), array('contentType' => 'text/vcard', 'version' => '3.0'), array('contentType' => 'text/x-vcard', 'version' => '2.1'))), '{http://sabredav.org/ns}read-only' => $readOnly);
     }
     return $books;
 }
All Usage Examples Of Turba::permissionsFilter