[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/inc/ -> class.vfs_prefixes.inc.php (source)

   1  <?php
   2    /***************************************************************************\
   3    * eGroupWare - File Manager                                                 *
   4    * http://www.egroupware.org                                                 *
   5    * Written by:                                                               *
   6    *  - Vinicius Cubas Brand <viniciuscb@users.sourceforge.net>                *
   7    *  sponsored by Thyamad - http://www.thyamad.com                            *
   8    * ------------------------------------------------------------------------- *
   9    * Description: File ID Prefixes class handler for SQL implementation v2     *
  10    * ------------------------------------------------------------------------- *
  11    *  This program is free software; you can redistribute it and/or modify it  *
  12    *  under the terms of the GNU General Public License as published by the    *
  13    *  Free Software Foundation; either version 2 of the License, or (at your   *
  14    *  option) any later version.                                               *
  15    \***************************************************************************/
  16  
  17      #12 Oct 2004 viniciuscb Initial Release
  18  
  19      define ('PHPGW_VFS2_PREFIX_APP','vfs2_prefix'); //for prefix
  20      define ('PHPGW_VFS2_PTYPE_APP','vfs2_ptype');   //for file type
  21  
  22      class vfs_prefixes
  23      {
  24          var $db;
  25  
  26          var $table_fields = array(
  27              'prefix_id',
  28              'owner_id',
  29              'prefix',
  30              'prefix_description',
  31              'prefix_type'
  32          );
  33  
  34  		function vfs_prefixes()
  35          {
  36              $this->db = clone($GLOBALS['phpgw']->db);
  37          }
  38  
  39          /*!
  40           * function add
  41           * @description Adds a new File ID Prefix to the prefixes repository
  42           * @param prefix string (required) A prefix
  43           * @param prefix_description string (optional) Brief prefix description
  44           * @param owner_id int (required) Owner Id of prefix
  45           * @param prefix_type (optional)  can be 'p' for prefix, 't' for type.
  46           * @result (int) prefix_id
  47           *
  48           * @note: will search for another equal $prefix in repository. If
  49           *        exists, returns its prefix_id and if user have permission,
  50           *        updates this prefix. If don't exists, insert and return its
  51           *        prefix_id. $dont_update will not do any update if exists, even
  52           *        if current user can update.
  53           */
  54  		function add($data,$dont_update=false)
  55          {
  56  
  57              if (!$data['prefix'])
  58              {
  59                  return false;
  60              }
  61  
  62              if (!$data['prefix_type'])
  63              {
  64                  $data['prefix_type'] = 'p';
  65              }
  66  
  67              //eliminate keys which are not a field in table
  68              foreach($data as $key => $val)
  69              {
  70                  if (!in_array($key,$this->table_fields))
  71                  {
  72                      unset($data[$key]);
  73                  }
  74              }
  75              
  76              //see if exists some equal prefix id
  77              $this->db->select('phpgw_vfs2_prefixes','prefix_id',array('prefix' => $data['prefix']));
  78              
  79              if($this->db->next_record()) //exists
  80              {
  81                  if ($dont_update)
  82                  {
  83                      return $this->db->Record['prefix_id'];
  84                  }
  85                  $data['prefix_id'] = $this->db->Record['prefix_id'];
  86                  return $this->edit($data);
  87              }
  88  
  89              if (!$data['owner_id'])
  90              {
  91                  $data['owner_id'] = $GLOBALS['phpgw_info']['user']['account_id'];
  92              }
  93  
  94              $this->db->insert('phpgw_vfs2_prefixes',$data,false,__LINE__,__FILE__);
  95  
  96              $this->db->select('phpgw_vfs2_prefixes','prefix_id',array('prefix' => $data['prefix']));
  97  
  98              if($this->db->next_record()) //exists
  99              {
 100                  return $this->db->Record['prefix_id'];
 101              }
 102  
 103              return false;
 104          }
 105  
 106          /*!
 107           * function edit
 108           * @description Edits a File ID Prefix
 109           * @param prefix_id int (required) The ID for prefix
 110           * @param prefix string (optional) A prefix
 111           * @param prefix_description string (optional) Brief prefix description
 112           * @param owner_id int (optional) Owner Id of prefix
 113           * @param prefix_type (optional)  can be 'p' for prefix, 't' for type.
 114           * @result (int) prefix_id
 115           * @result (bool) true on success, false on any other possibility
 116           */
 117  		function edit($data)
 118          {
 119              if (!$data['prefix_id'])
 120              {
 121                  return false;
 122              }
 123  
 124              //eliminate keys which are not a field in table
 125              foreach($data as $key => $val)
 126              {
 127                  if (!in_array($key,$this->table_fields))
 128                  {
 129                      unset($data[$key]);
 130                  }
 131              }
 132  
 133              $where['prefix_id'] = $data['prefix_id'];
 134              unset($data['prefix_id']);
 135  
 136              return $this->db->update('phpgw_vfs2_prefixes',$data,$where,__LINE__,__FILE__);
 137              
 138          }
 139  
 140          /*!
 141           * function remove
 142           * @description Removes a File ID Prefix
 143           * @param prefix_id int (required) The ID for prefix
 144           * @result (bool) true on success, false on any other possibility
 145           */
 146  		function remove($prefix_id)
 147          {
 148              return $this->db->delete('phpgw_vfs2_prefixes',array('prefix_id' => $prefix_id),__LINE__,__FILE__);
 149          }
 150  
 151          /*!
 152           * function get
 153           * @description Gets information about a prefix just based in prefix_id
 154           * @param prefix_id int (required) The ID for prefix
 155           *       OR
 156           * @param prefix int (required) The prefix
 157           * @result (array) with column names as indexes, empty array if inexist
 158           */
 159  		function get($data)
 160          {
 161              if (!$data['prefix_id'] && !$data['prefix'])
 162                  return false;
 163  
 164              
 165              $this->db->select('phpgw_vfs2_prefixes','*',$data,__LINE__,__FILE__);
 166  
 167              if ($this->db->next_record())
 168              {
 169                  return $this->db->Record;
 170              }
 171              return array();
 172          }
 173  
 174          /*!
 175           * function get_prefixes
 176           * @description Gets all prefixes this user can view, based in querying
 177           *   acl
 178           * @param user_id int (required) The ID of user to whom you want to
 179           *   know, or will get current user as default
 180           * @param status string  If 'view', returns info about all prefixes
 181           *   user can view. if 'owns', return only prefixes user owns
 182           * @result (array) with column names as indexes, empty array if inexist
 183           */
 184  		function get_prefixes($status='view',$user_id=false,$type='p')
 185          {
 186              if (!$user_id)
 187              {
 188                  $user_id = $GLOBALS['phpgw_info']['user']['account_id']; 
 189              }
 190  
 191              switch ($status)
 192              {
 193                  case 'owns':
 194                      $this->db->select('phpgw_vfs2_prefixes','*',array('owner_id'=>$user_id,'prefix_type'=>$type),__LINE__,__FILE__);
 195                      
 196                      while($this->db->next_record())
 197                      {
 198                          $return[] = $this->db->Record;
 199                      }
 200                      
 201                      break;
 202                  case 'view':
 203                      $acl =& CreateObject('phpgwapi.acl',$user_id);
 204  
 205                      //fetch ids of prefixes that user can view
 206                      if (!$pr_list =  $acl->get_location_list_for_id(PHPGW_VFS2_PREFIX_APP,PHPGW_ACL_READ,$user_id))
 207                      {
 208                          $pr_list = array();
 209                      }
 210  
 211                      //fetch ids of prefixes that groups user belong to can view
 212                      //Note: this will be in two phases. One: fetch groups user
 213                      //     belongs to. Two: fetch prefix list for these groups
 214  
 215                      /* Note: prefixes are organized in phpgwapi.acl in the
 216                       * following schema:
 217                       * - appname: (PHPGW_VFS2_PREFIX_APP)
 218                       * - location: id_prefix
 219                       * - account_id: Id of user that has grants (not the
 220                       *   grantor. The grantor is only the owner of prefix,
 221                       *   defined in prefixes repository).
 222                       * - acl_rights: PHPGW_ACL_READ
 223                       */
 224  
 225                      $user_groups = $GLOBALS['phpgw']->accounts->membership($user_id);
 226  
 227                      foreach($user_groups as $group)
 228                      {
 229                          if (!$group_pr_list = $acl->get_location_list_for_id(PHPGW_VFS2_PREFIX_APP,PHPGW_ACL_READ,$group['account_id']))
 230                          {
 231                              $group_pr_list = array();
 232                          }
 233  
 234                          $pr_list = array_merge($pr_list,$group_pr_list);
 235                      }
 236  
 237                      //remove dupliate values
 238                      $pr_list = array_unique($pr_list);
 239  
 240                      //now we have the list of prefixes user can view. We must
 241                      //now fetch complete information about prefixes of
 242                      //phpgw_vfs2_prefixes
 243  
 244                      if (!count($pr_list))
 245                      {
 246                          return array();
 247                      }
 248      
 249                      if ($pr_list)
 250                      {
 251                          $prefix_string = '('.implode(',',$pr_list).')';
 252                          
 253                          $this->db->select('phpgw_vfs2_prefixes','*','prefix_id IN '.$prefix_string." AND prefix_type='$type'",__LINE__,__FILE__);
 254                          
 255                          while($this->db->next_record())
 256                          {
 257                              $return[] = $this->db->Record;
 258                          }
 259                      }
 260                      else
 261                      {
 262                          return array();
 263                      }
 264  
 265                      break;
 266                  default:
 267                      return false;
 268              }
 269              return $return;
 270          }
 271  
 272          /*!
 273           * function update_permissions
 274           * @description Updates users who can see a prefix
 275           *
 276           * @param prefix_id int (required) The prefix that will have permissions
 277           *   changed 
 278           * @param user_list array  Array with account_ids that can read prefix
 279           *   as values.
 280           *
 281           * @result (bool)
 282           */
 283  		function update_permissions($prefix_id,$user_list)
 284          {
 285  
 286              //1. see if current user is owner of the prefix
 287              $current_user_id = $GLOBALS['phpgw_info']['user']['account_id'];
 288  
 289              $prefix_info = $this->get(array('prefix_id'=>$prefix_id));
 290  
 291              if ($current_user_id != $prefix_info['owner_id'])
 292              {
 293                  return false;
 294              }
 295  
 296              //2. get current permission for prefix
 297              $current_permissions = $this->get_permissions(array('prefix_id'=>$prefix_id));
 298  
 299              //3. change permissions
 300              $list_of_users_to_add = array_diff($user_list,$current_permissions);
 301              $list_of_users_to_del = array_diff($current_permissions,$user_list);
 302  
 303  
 304              $acl =& CreateObject('phpgwapi.acl',$current_user_id);
 305  
 306              foreach($list_of_users_to_add as $user_id)
 307              {
 308                  $acl->account_id = $user_id;
 309                  $acl->read_repository();
 310                  #echo "<br>\nAdded: prefix $prefix_id ; user $user_id";
 311                  $acl->add(PHPGW_VFS2_PREFIX_APP,$prefix_id,PHPGW_ACL_READ);
 312                  $acl->save_repository();
 313              }
 314  
 315              foreach($list_of_users_to_del as $user_id)
 316              {    
 317                  $acl->account_id = $user_id;
 318                  $acl->read_repository();
 319                  #echo "<br>\nDeleted: prefix $prefix_id ; user $user_id";
 320                  $acl->delete(PHPGW_VFS2_PREFIX_APP,$prefix_id);
 321                  $acl->save_repository();
 322              }
 323  
 324          }
 325  
 326          /*!
 327           * function get_permissions
 328           * @description This will get all permissions for a given prefix.
 329           *              In other words, will return an array of all accounts who
 330           *              can see a prefix. Will not dive into groups' users,
 331           *              only showing user and group accounts who can see
 332           *              prefix.
 333           *
 334           * @param prefix int (required) The File ID Prefix
 335           *    OR
 336           * @param prefix_id int (required) The ID of the File ID Prefix
 337           * @param prefixes array  The same type of the return of get_prefixes
 338           *
 339           * @result (array) with column names as indexes, empty array if inexist
 340           */
 341  		function get_permissions($data)
 342          {
 343              if (is_numeric($data))
 344              {
 345                  $prefix_id = $data;
 346              }
 347              elseif ($data['prefix'])
 348              {
 349                  $this->db->select('phpgw_vfs2_prefixes','prefix_id',array('prefix'=>$data['prefix']),__LINE__,__FILE__);
 350                  if ($this->db->next_record())
 351                  {
 352                      $prefix_id = $this->db->Record['prefix_id'];
 353                  }
 354              }
 355              elseif($data['prefix_id'])
 356              {
 357                  $prefix_id = $data['prefix_id'];
 358              }
 359  
 360              if (!$prefix_id)
 361              {
 362                  return false;
 363              }
 364  
 365              $acl =& CreateObject('phpgwapi.acl');
 366              $user_ids = $acl->get_ids_for_location($prefix_id,PHPGW_ACL_READ,PHPGW_VFS2_PREFIX_APP);
 367  
 368              return ($user_ids)?$user_ids:array();
 369          }
 370  
 371      }
 372  
 373  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7