PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/demo/ucenter/Lib/Model/RoleModel.class.php

http://mycaptain.googlecode.com/
PHP | 438 lines | 219 code | 144 blank | 75 comment | 40 complexity | 785a0a194826ff482d5d2af6ee31cbbe MD5 | raw file
  1. <?php
  2. /**
  3. // DoYouHaoBaby-X Ucenter ( DoYouHaoBaby-X ?? )
  4. // +---------------------------------------------------------------------
  5. //
  6. // “Copyright”
  7. // +---------------------------------------------------------------------
  8. // | (C) 2010 - 2011 http://doyouhaobaby.net All rights reserved.
  9. // | This is not a free software, use is subject to license terms
  10. // +---------------------------------------------------------------------
  11. //
  12. // “About This File”
  13. // +---------------------------------------------------------------------
  14. // | Ucenter ????
  15. // +---------------------------------------------------------------------
  16. */
  17. !defined( 'DYHB_PATH' ) && exit;
  18. class RoleModel extends CommonModel{
  19. /**
  20. * ???????
  21. */
  22. static public function init__(){
  23. return array(
  24. 'table_name' => 'role',
  25. 'props' => array(
  26. // ???????????????“???”
  27. 'role_id' => array( 'readonly' => true ),
  28. ),
  29. 'attr_protected' => 'role_id',
  30. 'check' => array(
  31. 'role_name'=>array(
  32. array( 'require',G::L( '???????' ) ),
  33. array( 'uniqueRoleName',G::L( '??????' ),'condition'=>'must','extend'=>'callback' ),
  34. ),
  35. 'role_parentid'=>array(
  36. array( 'uniqueRoleParentId',G::L( '????????' ),'condition'=>'must','extend'=>'callback' ),
  37. ),
  38. ),
  39. );
  40. }
  41. /**
  42. * ?????????????????????
  43. */
  44. static function F( ){
  45. $arrArgs = func_get_args();
  46. return ModelMeta::instance( __CLASS__ )->findByArgs( $arrArgs );
  47. }
  48. /**
  49. * ???? Model ???????
  50. */
  51. static function M( ){
  52. return ModelMeta::instance(__CLASS__);
  53. }
  54. /**
  55. * ????????
  56. */
  57. public function getParentRoleName( $nRoleId ){
  58. if( $nRoleId ==0 ) return;
  59. $oRole = self::F( $nRoleId )->getOne();
  60. if( !$oRole->isError( ) ){
  61. return $oRole->role_name;
  62. }
  63. }
  64. /**
  65. * ?????????
  66. */
  67. public function uniqueRoleName(){
  68. $nId = Safe::getGpc( 'id' );
  69. $sRoleName = trim( Safe::getGpc( 'role_name' ) ) ;
  70. $sRoleInfo='';
  71. if( $nId ){
  72. $arrRole = self::F( 'role_id =?',$nId )->asArray()->getOne( );
  73. $sRoleInfo = trim( $arrRole['role_name'] );
  74. }
  75. if( $sRoleName != $sRoleInfo ){
  76. $arrResult = self::F()->getByrole_name( $sRoleName )->toArray();
  77. if( !empty( $arrResult['role_id'] ) ) {
  78. return false;
  79. }
  80. else{
  81. return true;
  82. }
  83. }
  84. return true;
  85. }
  86. /**
  87. * ??????
  88. */
  89. public function uniqueRoleParentId(){
  90. $nRoleId = Safe::getGpc( 'id' );
  91. $nRoleParentId = Safe::getGpc('role_parentid');
  92. if( ( $nRoleId == $nRoleParentId ) && $nRoleId !='' && $nRoleParentId != '' ){
  93. return false;
  94. }
  95. return true;
  96. }
  97. /**
  98. * ???????
  99. */
  100. public function getGroupAppList( $nGroupId ){
  101. $oDb = $this->getDb();
  102. return $oDb->getAllRows( 'select b.node_id,b.node_title,b.node_name from '.
  103. AccessModel::F()->query()->getTablePrefix().'access as a ,'.
  104. NodeModel::F()->query()->getTablePrefix().'node as b where a.node_id=b.node_id and b.node_parentid=0 and a.role_id='.$nGroupId );
  105. }
  106. /**
  107. * ???????
  108. */
  109. public function delGroupApp( $nGroupId ){
  110. $oDb = $this->getDb();
  111. $bResult = $oDb->query( 'delete from '.AccessModel::F()->query()->getTablePrefix().'access where `access_level`=1 and role_id='.$nGroupId );
  112. if( $bResult===false ) {
  113. return false;
  114. }
  115. else {
  116. return true;
  117. }
  118. }
  119. /**
  120. * ???????
  121. */
  122. public function setGroupApps( $nGroupId,$arrAppIdList ){
  123. $oDb = $this->getDb();
  124. if(empty( $arrAppIdList )) {
  125. return true;
  126. }
  127. $sId = implode( ',',$arrAppIdList );
  128. $bResult = $oDb->query( 'INSERT INTO '.AccessModel::F()->query()->getTablePrefix().
  129. 'access (role_id,node_id,access_parentid,access_level) SELECT a.role_id, b.node_id,b.node_parentid,b.node_level FROM '.
  130. RoleModel::F()->query()->getTablePrefix().'role a, '.
  131. NodeModel::F()->query()->getTablePrefix().
  132. 'node b WHERE '.'a.role_id ='.$nGroupId.' AND b.node_id in('.$sId.')' );
  133. if( $bResult===false ) {
  134. return false;
  135. }
  136. else {
  137. return true;
  138. }
  139. }
  140. /**
  141. * ???????
  142. */
  143. public function getGroupModuleList( $nGroupId,$nAppId ){
  144. $oDb = $this->getDb();
  145. return $oDb->getAllRows( 'select b.node_id,b.node_title,b.node_name from '.
  146. AccessModel::F()->query()->getTablePrefix().'access as a ,'.
  147. NodeModel::F()->query()->getTablePrefix().
  148. 'node as b where a.node_id=b.node_id and b.node_parentid='.$nAppId.' and a.role_id='.$nGroupId );
  149. }
  150. /**
  151. * ???????
  152. */
  153. public function delGroupModule( $nGroupId,$nAppId ){
  154. $oDb = $this->getDb();
  155. $nResult = $oDb->query( 'delete from '.AccessModel::F()->query()->getTablePrefix().
  156. 'access where access_level=2 and access_parentid='.$nAppId.' and role_id='.$nGroupId );
  157. if( $nResult===false ) {
  158. return false;
  159. }
  160. else {
  161. return true;
  162. }
  163. }
  164. /**
  165. * ???????
  166. */
  167. public function setGroupModules( $nGroupId,$arrModuleIdList ){
  168. $oDb = $this->getDb();
  169. if( empty($arrModuleIdList )) {
  170. return true;
  171. }
  172. if( is_array( $arrModuleIdList ) ) {
  173. $arrModuleIdList = implode(',',$arrModuleIdList );
  174. }
  175. $bResult = $oDb->query( 'INSERT INTO '.AccessModel::F()->query()->getTablePrefix().
  176. 'access (role_id,node_id,access_parentid,access_level) SELECT a.role_id, b.node_id,b.node_parentid,b.node_level FROM '.
  177. RoleModel::F()->query()->getTablePrefix().'role a, '.NodeModel::F()->query()->getTablePrefix().
  178. 'node b WHERE '.'a.role_id ='.$nGroupId.' AND b.node_id in('.$arrModuleIdList.')' );
  179. if( $bResult===false) {
  180. return false;
  181. }
  182. else {
  183. return true;
  184. }
  185. }
  186. /**
  187. * ???????
  188. */
  189. public function getGroupActionList( $nGroupId,$nModuleId ){
  190. $oDb = $this->getDb();
  191. return $oDb->getAllRows( 'select b.node_id,b.node_title,b.node_name from '.
  192. AccessModel::F()->query()->getTablePrefix().'access as a ,'.
  193. NodeModel::F()->query()->getTablePrefix().'node as b where a.node_id=b.node_id and b.node_parentid='.$nModuleId.' and a.role_id='.$nGroupId );
  194. }
  195. /**
  196. * ?????
  197. */
  198. public function delGroupAction( $nGroupId,$nModuleId ){
  199. $oDb = $this->getDb();
  200. $bResult = $oDb->query( 'delete from '.AccessModel::F()->query()->getTablePrefix().
  201. 'access where access_level=3 and access_parentid='.$nModuleId.' and role_id='.$nGroupId );
  202. if($bResult===false) {
  203. return false;
  204. }
  205. else {
  206. return true;
  207. }
  208. }
  209. /**
  210. * ?????
  211. */
  212. public function setGroupActions( $nGroupId,$arrActionIdList ){
  213. $oDb = $this->getDb();
  214. if(empty( $arrActionIdList )) {
  215. return true;
  216. }
  217. if(is_array( $arrActionIdList )) {
  218. $arrActionIdList = implode(',',$arrActionIdList);
  219. }
  220. $bResult = $oDb->query('INSERT INTO '.AccessModel::F()->query()->getTablePrefix().
  221. 'access (role_id,node_id,access_parentid,access_level) SELECT a.role_id, b.node_id,b.node_parentid,b.node_level FROM '.
  222. RoleModel::F()->query()->getTablePrefix().'role a, '.
  223. NodeModel::F()->query()->getTablePrefix().'node b WHERE '.'a.role_id ='.$nGroupId.' AND b.node_id in('.$arrActionIdList.')' );
  224. if( $bResult===false ) {
  225. return false;
  226. }
  227. else {
  228. return true;
  229. }
  230. }
  231. /**
  232. * ???????
  233. */
  234. public function getGroupUserList( $nGroupId ){
  235. $oDb = $this->getDb();
  236. return $oDb->getAllRows( 'select b.user_id,b.user_nikename,b.user_email from '.
  237. userroleModel::F()->query()->getTablePrefix().'userrole as a ,'.
  238. UserModel::F()->query()->getTablePrefix().'user as b where a.user_id=b.user_id and a.role_id='.$nGroupId );
  239. }
  240. /**
  241. * ???????
  242. */
  243. public function delGroupUser( $nGroupId ){
  244. $oDb = $this->getDb();
  245. $bResult = $oDb->query( 'delete from '.userroleModel::F()->query()->getTablePrefix().'userrole where role_id='.$nGroupId );
  246. if( $bResult === false ) {
  247. return false;
  248. }
  249. else {
  250. return true;
  251. }
  252. }
  253. /**
  254. * ???????
  255. */
  256. public function setGroupUsers( $nGroupId,$arrUserIdList ){
  257. $oDb = $this->getDb();
  258. if( empty( $arrUserIdList ) ) {
  259. return true;
  260. }
  261. if( is_string($arrUserIdList ) ) {
  262. $arrUserIdList = explode( ',',$arrUserIdList );
  263. }
  264. array_walk( $arrUserIdList, array( $this, 'fieldFormat' ));
  265. $arrUserIdList = implode(',',$arrUserIdList);
  266. $bResult = $oDb->query( 'INSERT INTO '.userroleModel::F()->query()->getTablePrefix().
  267. 'userrole (role_id,user_id) SELECT a.role_id, b.user_id FROM '.
  268. RoleModel::F()->query()->getTablePrefix().'role a, '.
  269. UserModel::F()->query()->getTablePrefix().'user b WHERE '.'a.role_id ='.$nGroupId.' AND b.user_id in('.$arrUserIdList.')' );
  270. if( $bResult===false ) {
  271. return false;
  272. }
  273. else {
  274. return true;
  275. }
  276. }
  277. /**
  278. * ???????
  279. */
  280. protected function fieldFormat( &$Value ){
  281. if(is_int( $Value )) {
  282. $Value = intval( $Value );
  283. }
  284. else if( is_float( $Value ) ) {
  285. $Value = floatval( $Value );
  286. }
  287. else if(is_string( $Value )) {
  288. $Value = '"'.addslashes( $Value ).'"';
  289. }
  290. return $Value;
  291. }
  292. }