PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/packages/auth/classes/auth/group/driver.php

https://bitbucket.org/codeyash/bootstrap
PHP | 127 lines | 62 code | 19 blank | 46 comment | 6 complexity | 2b6bc45d6a4040458146f282ba3d20e1 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Auth;
  13. abstract class Auth_Group_Driver extends \Auth_Driver
  14. {
  15. /**
  16. * @var Auth_Driver
  17. */
  18. protected static $_instance = null;
  19. /**
  20. * @var array contains references if multiple were loaded
  21. */
  22. protected static $_instances = array();
  23. public static function forge(array $config = array())
  24. {
  25. // default driver id to driver name when not given
  26. ! array_key_exists('id', $config) && $config['id'] = $config['driver'];
  27. $class = \Inflector::get_namespace($config['driver']).'Auth_Group_'.ucfirst(\Inflector::denamespace($config['driver']));
  28. $driver = new $class($config);
  29. static::$_instances[$driver->get_id()] = $driver;
  30. is_null(static::$_instance) and static::$_instance = $driver;
  31. foreach ($driver->get_config('drivers', array()) as $type => $drivers)
  32. {
  33. foreach ($drivers as $d => $custom)
  34. {
  35. $custom = is_int($d)
  36. ? array('driver' => $custom)
  37. : array_merge($custom, array('driver' => $d));
  38. $class = 'Auth_'.ucfirst($type).'_Driver';
  39. $class::forge($custom);
  40. }
  41. }
  42. return $driver;
  43. }
  44. // ------------------------------------------------------------------------
  45. /**
  46. * Verify Acl access
  47. *
  48. * @param mixed condition to validate
  49. * @param string acl driver id or null to check all
  50. * @param array user identifier to check in form array(driver_id, user_id)
  51. * @return bool
  52. */
  53. public function has_access($condition, $driver, $group = null)
  54. {
  55. // When group was given just check the group
  56. if (is_array($group))
  57. {
  58. if ($driver === null)
  59. {
  60. foreach (\Auth::acl(true) as $acl)
  61. {
  62. if ($acl->has_access($condition, $group))
  63. {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. return \Auth::acl($driver)->has_access($condition, $group);
  70. }
  71. // When no group was given check all logged in users
  72. foreach (\Auth::verified() as $v)
  73. {
  74. // ... and check all those their groups
  75. $gs = $v->get_groups();
  76. foreach ($gs as $g_id)
  77. {
  78. // ... and try to validate if its group is this one
  79. if ($this instanceof $g_id[0])
  80. {
  81. if ($this->has_access($condition, $driver, $g_id))
  82. {
  83. return true;
  84. }
  85. }
  86. }
  87. }
  88. // when nothing validated yet: it has failed to
  89. return false;
  90. }
  91. // ------------------------------------------------------------------------
  92. /**
  93. * Check membership of given users
  94. *
  95. * @param mixed condition to check for access
  96. * @param array user identifier in the form of array(driver_id, user_id), or null for logged in
  97. * @return bool
  98. */
  99. abstract public function member($group, $user = null);
  100. /**
  101. * Fetch the display name of the given group
  102. *
  103. * @param mixed group condition to check
  104. * @return string
  105. */
  106. abstract public function get_name($group);
  107. }
  108. /* end of file driver.php */