shopaholic /application/modules/Authorization/Model/DbTable/Allow.php

Language PHP Lines 538
MD5 Hash dd88cec1f769a53d9f53e0f2a9c083c8
Repository https://github.com/shopaholiccompany/shopaholic.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<?php
/**
 * SocialEngine
 *
 * @category   Application_Core
 * @package    Authorization
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: Allow.php 7244 2010-09-01 01:49:53Z john $
 * @author     John
 */

/**
 * @category   Application_Core
 * @package    Authorization
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class Authorization_Model_DbTable_Allow extends Engine_Db_Table
  implements Authorization_Model_AdapterInterface
{
  public function getAdapterName()
  {
    return 'context';
  }

  public function getAdapterPriority()
  {
    return 50;
  }


  /**
   * Valid relationship types. Ordered by speed of calculation
   *
   * @var array
   */
  protected $_relationships = array(
    'everyone',
    'registered',
    'member',
    'member_requested',
    'owner',
    'parent',
    'owner_member',
    'member_member',
    'network',
    'owner_member_member',
    'owner_network'
  );

  public function isAllowed($resource, $role, $action)
  {
    // Resource must be an instance of Core_Model_Item_Abstract
    if( !($resource instanceof Core_Model_Item_Abstract) )
    {
      // We have nothing to say about generic permissions
      return Authorization_Api_Core::LEVEL_IGNORE;
    }

    // Role must be an instance of Core_Model_Item_Abstract or a string relationship type
    if( !($role instanceof Core_Model_Item_Abstract) && !is_string($role) )
    {
      // Disallow access to unknown role types
      return Authorization_Api_Core::LEVEL_DISALLOW;
    }

    // Owner can do what they want with the resource
    if( ($role instanceof Core_Model_Item_Abstract && method_exists($resource, 'isOwner') && $resource->isOwner($role)) || $role === 'owner' )
    {
      return Authorization_Api_Core::LEVEL_ALLOW;
    }

    // Now go over set permissions
    // @todo allow for custom types
    $rowset = $this->_getAllowed($resource, $role, $action);

    if( is_null($rowset) || !($rowset instanceof Engine_Db_Table_Rowset) )
    {
      // No permissions have been defined for resource, disallow all
      return Authorization_Api_Core::LEVEL_DISALLOW;
    }

    // Index by type
    $perms = array();
    $permsByOrder = array();
    $items = array();
    foreach( $rowset as $row )
    {
      if( empty($row->role_id) )
      {
        $index = array_search($row->role, $this->_relationships);
        if( $index === false ) { // Invalid type
          continue;
        }
        $perms[$row->role] = $row;
        $permsByOrder[$index] = $row->role;
      }
      else
      {
        $items[] = $row;
      }
    }

    // We we're passed a type role, how convenient
    if( is_string($role) )
    {
      if( isset($perms[$role]) && is_object($perms[$role]) && $perms[$role]->value == Authorization_Api_Core::LEVEL_ALLOW )
      {
        return Authorization_Api_Core::LEVEL_ALLOW;
      }
      else
      {
        return Authorization_Api_Core::LEVEL_DISALLOW;
      }
    }

    // Scan available types
    foreach( $permsByOrder as $perm => $type ) {
      $row = $perms[$type];
      $method = 'is_' . $type;
      if( !method_exists($this, $method) ) continue;
      $applies = $this->$method($resource, $role);
      if( $applies && $row->value == Authorization_Api_Core::LEVEL_ALLOW ) {
        return Authorization_Api_Core::LEVEL_ALLOW;
      }
    }

    // Ok, lets check the items then
    foreach( $items as $item ) {
      if( !Engine_Api::_()->hasItemType($row->role) )
      {
        continue;
      }

      // Item itself is auth'ed
      if( is_object($role) && $role->getType() == $row->role && $role->getIdentity() == $row->role_id )
      {
          return Authorization_Api_Core::LEVEL_ALLOW;
      }

      // Get item class
      $itemClass = Engine_Api::_()->getItemClass($row->role);

      // Member of
      if( method_exists($itemClass, 'membership') )
      {
        $item = Engine_Api::_()->getItem($row->role, $row->role_id);
        if( $item && $item->membership()->isMember($role, null, $row->subgroup_id))
        {
          return Authorization_Api_Core::LEVEL_ALLOW;
        }
      }

      // List
      else if( method_exists($itemClass, 'has') )
      {
        $item = Engine_Api::_()->getItem($row->role, $row->role_id);
        if( $item && $item->has($role) )
        {
          return Authorization_Api_Core::LEVEL_ALLOW;
        }
      }
    }
    
    return Authorization_Api_Core::LEVEL_DISALLOW;
  }

  public function getAllowed($resource, $role, $action)
  {
    // Non-boolean values are not yet implemented
    return $this->isAllowed($resource, $role, $action);
  }

  public function setAllowed($resource, $role, $action, $value = false, $role_id = 0)
  {
    // Can set multiple actions
    if( is_array($action) )
    {
      foreach( $action as $key => $value )
      {
        $this->setAllowed($resource, $role, $key, $value, $role_id);
      }

      return $this;
    }

    // Resource must be an instance of Core_Model_Item_Abstract
    if( !($resource instanceof Core_Model_Item_Abstract) )
    {
      throw new Authorization_Model_Exception('$resource must be an instance of Core_Model_Item_Abstract');
    }

    // Role must be a string, NOT a Core_Model_Item_Abstract
    //if( !is_string($role) )
    //{
    //  throw new Authorization_Model_Exception('$role must be a string relationship type');
    //}

    // Ignore owner (since owner is allowed everything)
    if( $role === 'owner' ) {
      return $this;
    }

    if( is_string($role) ) {
      $role_id = 0;
    } else if( $role instanceof Core_Model_Item_Abstract ) {
      $role_id = $role->getIdentity();
      $role = $role->getType();
    }

    // Try to get an existing row
    $select = $this->select()
      ->where('resource_type = ?', $resource->getType())
      ->where('resource_id = ?', $resource->getIdentity())
      ->where('action = ?', $action)
      ->where('role = ?', $role)
      ->where('role_id = ?', $role_id)
      ->limit(1);

    $row = $this->fetchRow($select);

    $deleteOnDisallow = true;

    // Whoops, create a new row)
    if( null === $row && (!$deleteOnDisallow || $value) )
    {
      $row = $this->createRow();
      $row->resource_type = $resource->getType();
      $row->resource_id = $resource->getIdentity();
      $row->action = $action;
      $row->role = $role;
      $row->role_id = $role_id;
    }

    if( null !== $row ) {
      if( !$deleteOnDisallow || $value ) {
        $row->value = (bool) $value;
        $row->save();
      } else if( $deleteOnDisallow && !$value ) {
        $row->delete();
      }
    }

    return $this;
  }

  protected function _getAllowed($resource, $role, $action)
  {
    // Make sure resource has an id (that it exists)
    $resource_type = $this->_getResourceType($resource);
    $resource_id = $this->_getResourceIdentity($resource);
    if( is_null($resource_id) )
    {
      return null;
    }

    // Get permissions
    $select = $this->select()
      ->where('resource_type = ?', $resource_type)
      ->where('resource_id = ?', $resource_id)
      ->where('action = ?', $action);
      //->where('role_type = ?', $role_guid[0])
      //->where('role_id = ?', $role_guid[1])

    return $this->fetchAll($select);
  }




  // Calculators

  // Tier 1

  public function is_everyone($resource, $role)
  {
    return true;
  }

  public function is_registered($resource, $role)
  {
    if( $role === 'registered' ) {
      return true;
    }
    if( !$role instanceof Core_Model_Item_Abstract ) {
      return false;
    }
    return (bool) $role->getIdentity();
  }

  public function is_member($resource, $role)
  {
    if( $role === 'member' ) {
      return true;
    }
    //if( !$role instanceof Core_Model_Item_Abstract ) {
    if( !$role instanceof User_Model_User ) {
      return false;
    }
    if( !method_exists($resource, 'membership') ) {
      return false;
    }
    return $resource->membership()->isMember($role, true);
  }

  public function is_member_requested($resource, $role)
  {
    if( $role === 'member_requested' ) {
      return true;
    }
    //if( !$role instanceof Core_Model_Item_Abstract ) {
    if( !$role instanceof User_Model_User ) {
      return false;
    }
    if( !method_exists($resource, 'membership') ) {
      return false;
    }
    $info = $resource->membership()->getMemberInfo($role);
    return ( null !== $info && $info->resource_approved && !$info->active );
  }

  public function is_owner($resource, $role)
  {
    if( $role === 'owner' ) {
      return true;
    }
    if( !$role instanceof Core_Model_Item_Abstract ) {
      return false;
    }
    return $role->isSelf($resource->getOwner());
  }

  public function is_parent($resource, $role)
  {
    if( $role === 'parent' ) {
      return true;
    }
    if( !$role instanceof Core_Model_Item_Abstract ) {
      return false;
    }
    return $role->isSelf($resource->getParent());
  }

  // Tier 2

  public function is_owner_member($resource, $role)
  {
    if( $role === 'owner_member' ) {
      return true;
    }
    if( !($role instanceof Core_Model_Item_Abstract) || !($role instanceof User_Model_User) ) {
      return false;
    }
    $owner = $resource->getOwner();
    if( !method_exists($owner, 'membership') ) {
      return false;
    }
    return $owner->membership()->isMember($role, true);
  }

  public function is_parent_member($resource, $role)
  {
    if( $role === 'parent_member' ) {
      return true;
    }
    if( !($role instanceof Core_Model_Item_Abstract) || !($role instanceof User_Model_User) ) {
      return false;
    }
    $parent = $resource->getParent();
    if( !method_exists($parent, 'membership') ) {
      return false;
    }
    return $parent->membership()->isMember($role, true);
  }

  public function is_member_member($resource, $role)
  {
    // Note: this implies is_member
    if( $role === 'member_member' || $role == 'member' ) {
      return true;
    }
    if( !($role instanceof Core_Model_Item_Abstract) || !($role instanceof User_Model_User) ) {
      return false;
    }
    if( !method_exists($resource, 'membership') ) {
      return false;
    }

    if( $this->is_member($resource, $role) ) {
      return true;
    }

    $resourceMembershipTable = $resource->membership()->getReceiver();
    $userMembershipTable = $role->membership()->getReceiver();

    // Build
    $resourceMembershipTableName = $resourceMembershipTable->info('name');
    $userMembershipTableName = $userMembershipTable->info('name');

    $select = new Zend_Db_Select($resourceMembershipTable->getAdapter());
    $select
      ->from($resourceMembershipTableName, 'user_id')
      ->join($userMembershipTableName, "`{$resourceMembershipTableName}`.`user_id`=`{$userMembershipTableName}_2`.resource_id", null)
      ->where("`{$resourceMembershipTableName}`.resource_id = ?", $resource->getIdentity())
      ->where("`{$userMembershipTableName}_2`.user_id = ?", $role->getIdentity())
      ;

    $data = $select->query()->fetch();
    return !empty($data);
  }

  public function is_network($resource, $role)
  {
    if( $role === 'network' ) {
      return true;
    }
    if( !($role instanceof Core_Model_Item_Abstract) || !($role instanceof User_Model_User) ) {
      return false;
    }
    if( !($resource instanceof User_Model_User) ) {
      return false;
    }
    $networkMembershipTable = Engine_Api::_()->getDbtable('membership', 'network');
    $networkMembershipName = $networkMembershipTable->info('name');

    $select = new Zend_Db_Select($networkMembershipTable->getAdapter());
    $select
      ->from($networkMembershipName, 'user_id')
      ->join($networkMembershipName, "`{$networkMembershipName}`.`resource_id`=`{$networkMembershipName}_2`.resource_id", null)
      ->where("`{$networkMembershipName}`.user_id = ?", $resource->getIdentity())
      ->where("`{$networkMembershipName}_2`.user_id = ?", $role->getIdentity())
      ;

    $data = $select->query()->fetch();
    return !empty($data);
  }


  // Tier 3

  public function is_owner_member_member($resource, $role)
  {
    // Note: this implies is_owner_member
    if( $role === 'owner_member_member' || $role == 'owner_member' ) {
      return true;
    }
    if( !($role instanceof Core_Model_Item_Abstract) || !($role instanceof User_Model_User) ) {
      return false;
    }
    $owner = $resource->getOwner();
    return $this->is_member_member($owner, $role); // should work
  }

  public function is_parent_member_member($resource, $role)
  {
    // Note: this implies is_owner_member
    if( $role === 'parent_member_member' || $role == 'parent_member' ) {
      return true;
    }
    if( !($role instanceof Core_Model_Item_Abstract) || !($role instanceof User_Model_User) ) {
      return false;
    }
    $parent = $resource->getParent();
    return $this->is_member_member($parent, $role); // should work
  }

  public function is_owner_network($resource, $role)
  {
    if( $role === 'owner_network' ) {
      return true;
    }
    if( !($role instanceof Core_Model_Item_Abstract) || !($role instanceof User_Model_User) ) {
      return false;
    }
    $owner = $resource->getOwner();
    return $this->is_network($owner, $role); // should work
  }


  
  // Utility
  
  protected function _getResourceType($resource)
  {
    if( is_string($resource) )
    {
      return $resource;
    }

    else if( is_array($resource) && isset($resource[0]) )
    {
      return $resource[0];
    }

    else if( $resource instanceof Core_Model_Item_Abstract )
    {
      return $resource->getType();
    }

    else
    {
      return null;
    }
  }

  /**
   * Returns the identity of a resource from several possible formats:
   * Core_Model_Item_Abstract->getIdentity()
   * integer
   * array(type, identity)
   *
   * @param mixed $resource
   * @return mixed The identity of the resource
   */
  protected function _getResourceIdentity($resource)
  {
    if( is_numeric($resource) )
    {
      return $resource;
    }

    else if( is_array($resource) && isset($resource[1]) )
    {
      return $resource[1];
    }

    else if( $resource instanceof Core_Model_Item_Abstract )
    {
      return $resource->getIdentity();
    }

    else
    {
      return null;
    }
  }
}
Back to Top