CMongoDbAuthManager.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. *
  4. * CMongoDbAuthManager
  5. *
  6. * A quick hack that extends CPhpAuthManager
  7. * and overrides loadFromFile, saveToFile
  8. * The file on disk is replaced by a collection in a mongodb
  9. *
  10. *
  11. * PHP version 5.2+
  12. * required extensions: YiiMongoDbSuite
  13. *
  14. * @author Joe Blocher <yii@myticket.at>
  15. * @copyright 2011 myticket it-solutions gmbh
  16. * @license New BSD License
  17. * @category Database
  18. * @version 0.6 alpha
  19. *
  20. */
  21. class CMongoDbAuthManager extends CPhpAuthManager {
  22. const DEFAULT_CONFIG = 'default';
  23. public $mongoConnectionId = 'mongodb';
  24. //The authFile property is the collection name
  25. public $authFile = 'mongodb_authmanager';
  26. public $super_admin = 'administrator';
  27. private $_id; //the MongoId
  28. private $_configId;
  29. protected static $_emongoDb;
  30. protected static $_mongocollections = array();
  31. /**
  32. * MongoCmsAuthManager::__construct()
  33. *
  34. * @param mixed $configId
  35. */
  36. public function __construct($configId = null)
  37. {
  38. $this->configId = $configId;
  39. }
  40. /**
  41. * Set the configId
  42. * Set configId to 'default' if is empty
  43. *
  44. * @param string $configId
  45. */
  46. public function setConfigId($configId)
  47. {
  48. $this->_configId = empty($configId) ? self::DEFAULT_CONFIG : $configId;
  49. }
  50. /**
  51. * Get the configId
  52. *
  53. * @return string
  54. */
  55. public function getConfigId()
  56. {
  57. return $this->_configId;
  58. }
  59. /**
  60. * Switch to another config
  61. *
  62. * @param mixed $configId
  63. */
  64. public function switchConfig($configId = null, $loadData = false)
  65. {
  66. $this->configId = $configId;
  67. $this->_id = null;
  68. if ($loadData)
  69. $this->init(); //load from mongodb
  70. }
  71. /**
  72. * 检查是否是最后一个超级管理员
  73. */
  74. public function checkLastSuperAdmin(){
  75. $item = $this->loadFromFile($this->authFile);
  76. if (isset($item[$this->super_admin])){
  77. $super_assignments = $item[$this->super_admin]['assignments'];
  78. if (count($super_assignments) <= 1){
  79. return true;
  80. } else {
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86. /**
  87. * 返回包含指定权限的用户列表
  88. */
  89. public function getAuthUser($name){
  90. $item = $this->loadFromFile($this->authFile);
  91. $user_ids = array();
  92. if (isset($item[$name]) && isset($item[$name]['assignments'])){
  93. $assignments = $item[$name]['assignments'];
  94. $user_ids = array_keys($assignments);
  95. }
  96. return $user_ids;
  97. }
  98. /**
  99. * Get raw MongoDB instance
  100. *
  101. * @return MongoDB
  102. */
  103. public function getDb()
  104. {
  105. if (self::$_emongoDb === null)
  106. self::$_emongoDb = Yii::app()->getComponent($this->mongoConnectionId);
  107. return self::$_emongoDb->getDbInstance();
  108. }
  109. /**
  110. * Returns current MongoCollection object
  111. * By default this method use {@see authFile}
  112. *
  113. * @return MongoCollection
  114. */
  115. public function getCollection($name = null)
  116. {
  117. if (!isset($name))
  118. $name = $this->authFile;
  119. if (!isset(self::$_mongocollections[$name]))
  120. self::$_mongocollections[$name] = $this->getDb()->selectCollection($name);
  121. return self::$_mongocollections[$name];
  122. }
  123. /**
  124. * Loads the authorization data from mongo db
  125. *
  126. * @param string $file is the collection name
  127. * @return array the authorization data
  128. * @see saveToFile
  129. */
  130. protected function loadFromFile($file)
  131. {
  132. $collection = $this->getCollection($file);
  133. $criteria = array('configId' => $this->configId);
  134. $data = $collection->findOne($criteria);
  135. if (empty($data))
  136. return array();
  137. // remove _id from data, because it's not an AuthItem
  138. if (isset($data['_id']))
  139. {
  140. $this->_id = $data['_id'];
  141. unset($data['_id']);
  142. }
  143. // remove configId from data, because it's not an AuthItem
  144. if (isset($data['configId']))
  145. {
  146. $this->configId = $data['configId'];
  147. unset($data['configId']);
  148. }
  149. return $data;
  150. }
  151. /**
  152. * Saves the authorization data from the collection 'file'
  153. *
  154. * @param array $data the authorization data
  155. * @param string $file the collection name
  156. * @see loadFromFile
  157. */
  158. protected function saveToFile($data, $file)
  159. {
  160. $collection = $this->getCollection($file);
  161. //have to set the _id for scenario update
  162. if (isset($this->_id))
  163. $data['_id'] = new MongoId($this->_id);
  164. $data['configId'] = $this->configId;
  165. $collection->save($data);
  166. //if this is a new record the _id value is created
  167. //assign $this->_id is important when authManager->save() is called more than once
  168. $this->_id = $data['_id'];
  169. }
  170. }
  171. ?>