/pnadmin.php

https://github.com/intraweb-modules12/iw_myrole · PHP · 263 lines · 163 code · 43 blank · 57 comment · 20 complexity · c285d3b533b3169261da5bedf5d893b4 MD5 · raw file

  1. <?php
  2. /**
  3. * Main admin function. Create admin interface
  4. * @author: Josep Ferràndiz (jferran6@xtec.cat)
  5. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  6. * @param: Array with the id of the group that can change roles
  7. * @return: Admin main page
  8. */
  9. function iw_myrole_admin_main($args)
  10. {
  11. $dom = ZLanguage::getModuleDomain('iw_myrole');
  12. $gid = FormUtil::getPassedValue('gid', isset($args['gid']) ? $args['gid'] : null, 'POST');
  13. // Security check
  14. if (!SecurityUtil::checkPermission('iw_myrole::', "::", ACCESS_ADMIN)) {
  15. return LogUtil::registerError(__('Sorry! No authorization to access this module.', $dom), 403);
  16. }
  17. $groupsNotChangeable = pnModGetVar('iw_myrole','groupsNotChangeable');
  18. $sv = pnModFunc('iw_main', 'user', 'genSecurityValue');
  19. $groups = pnModFunc('iw_main', 'user', 'getAllGroups', array('sv' => $sv,
  20. 'less' => pnModGetVar('iw_myrole', 'rolegroup')));
  21. foreach($groups as $group){
  22. $checked = false;
  23. if(strpos($groupsNotChangeable,'$'.$group['id'].'$') != false){
  24. $checked = true;
  25. }
  26. $groupsArray[] = array('id' => $group['id'],
  27. 'name' => $group['name'],
  28. 'checked' => $checked);
  29. }
  30. $pnRender = pnRender::getInstance('iw_myrole',false);
  31. // Gets the groups
  32. $sv = pnModFunc('iw_main', 'user', 'genSecurityValue');
  33. $groups = pnModFunc('iw_main', 'user', 'getAllGroups', array('sv' => $sv));
  34. $pnRender -> assign('roleGroup', pnModGetVar('iw_myrole', 'rolegroup'));
  35. $pnRender -> assign('groups', $groups);
  36. $pnRender -> assign('groupsArray', $groupsArray);
  37. return $pnRender -> fetch('iw_myrole_admin_main.htm');
  38. }
  39. /**
  40. * Show module information
  41. * @author: Josep Ferràndiz (jferran6@xtec.cat)
  42. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  43. * @param: none
  44. * @return: Module information
  45. */
  46. // Informació del mòdul
  47. function iw_myrole_admin_module()
  48. {
  49. $dom = ZLanguage::getModuleDomain('iw_myrole');
  50. // Security check
  51. if (!SecurityUtil::checkPermission('iw_myrole::', "::", ACCESS_ADMIN)) {
  52. return LogUtil::registerError(__('Sorry! No authorization to access this module.', $dom), 403);
  53. }
  54. $module = pnModFunc('iw_main', 'user', 'module_info', array('module_name' => 'iw_myrole',
  55. 'type' => 'admin'));
  56. $pnRender = pnRender::getInstance('iw_myrole',false);
  57. $pnRender -> assign('module', $module);
  58. return $pnRender -> fetch('iw_myrole_admin_module.htm');
  59. }
  60. /**
  61. * Change the group that can change roles
  62. * @author: Josep Ferràndiz (jferran6@xtec.cat)
  63. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  64. * @param: Array with the id of the group that can change roles
  65. * @return:
  66. */
  67. // Canvia a la taula de permisos els relatius al iw_myrole
  68. function iw_myrole_admin_changeGroup($args){
  69. $dom = ZLanguage::getModuleDomain('iw_myrole');
  70. $gid = FormUtil::getPassedValue('gid', isset($args['gid']) ? $args['gid'] : null, 'POST');
  71. $groups = FormUtil::getPassedValue('groups', isset($args['groups']) ? $args['groups'] : null, 'POST');
  72. // Security check
  73. if (!SecurityUtil::checkPermission('iw_myrole::', "::", ACCESS_ADMIN)) {
  74. return LogUtil::registerError(__('Sorry! No authorization to access this module.', $dom), 403);
  75. }
  76. // Confirm authorisation code
  77. if (!SecurityUtil::confirmAuthKey()) {
  78. return LogUtil::registerAuthidError (pnModURL('iw_myrole', 'admin', 'main'));
  79. }
  80. $groupsString = '$';
  81. foreach($groups as $group){
  82. $groupsString .= '$'.$group.'$';
  83. }
  84. if ($gid) {
  85. // Modify the permissions in group_perms
  86. $changePerms = pnModApiFunc ('iw_myrole', 'admin', 'changePermissions', array('gid'=> $gid));
  87. if ($changePerms) {
  88. //Update module var with new value
  89. pnModSetVar('iw_myrole', 'rolegroup', $gid);
  90. Logutil::registerStatus(__('The group change has been made.', $dom));
  91. } else {
  92. Logutil::registerError(__('The group change has not been made.', $dom));
  93. }
  94. }
  95. pnModSetVar('iw_myrole','groupsNotChangeable', $groupsString);
  96. return pnRedirect(pnModURL('iw_myrole', 'admin', 'main'));
  97. }
  98. /**
  99. * Change user groups
  100. * @author: Josep Ferràndiz (jferran6@xtec.cat)
  101. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  102. * @param: args Array with the groups id's
  103. * @return: True if success or false otherwise
  104. */
  105. function iw_myrole_admin_changeRole($args)
  106. {
  107. $dom = ZLanguage::getModuleDomain('iw_myrole');
  108. // Get the parameters
  109. $roles = FormUtil::getPassedValue('roles', isset($args['roles']) ? $args['roles'] : null, 'POST');
  110. $setDefault = FormUtil::getPassedValue('setDefault', isset($args['setDefault']) ? $args['setDefault'] : null, 'POST');
  111. // Security check
  112. if (!SecurityUtil::checkPermission('iw_myrole::', "::", ACCESS_ADMIN)) {
  113. return LogUtil::registerError(__('Sorry! No authorization to access this module.', $dom), 403);
  114. }
  115. //Check if the group that can change roles have admin permisions. If not the block is not showed
  116. $correctGroupPermissions = pnModAPIFunc('iw_myrole', 'admin', 'correctGroupPermissions');
  117. if(!$correctGroupPermissions){
  118. $sv = pnModFunc('iw_main', 'user', 'genSecurityValue');
  119. pnModFunc('iw_main', 'user', 'userSetVar', array('uid' => pnUserGetVar('uid'),
  120. 'name' => 'invalidChange',
  121. 'module' => 'iw_myrole',
  122. 'lifetime' => 10,
  123. 'nult' => true,
  124. 'value' => 1,
  125. 'sv' => $sv));
  126. return pnRedirect($_SERVER['HTTP_REFERER']);
  127. }
  128. $uid = pnUserGetVar('uid');
  129. //get the headlines saved in the user vars. It is renovate every 10 minutes
  130. $sv = pnModFunc('iw_main', 'user', 'genSecurityValue');
  131. $exists = pnModApiFunc('iw_main', 'user', 'userVarExists', array('name' => 'defaultRoles',
  132. 'module' => 'iw_myrole',
  133. 'uid' => $uid,
  134. 'sv' => $sv));
  135. if(!$exists){
  136. //get user groups
  137. $sv = pnModFunc('iw_main', 'user', 'genSecurityValue');
  138. $userGroups = pnModFunc('iw_main', 'user', 'getAllUserGroups', array('sv'=> $sv,
  139. 'uid'=> $uid));
  140. $i = 0;
  141. foreach($userGroups as $group){
  142. $groups .= $group['id'].'$$';
  143. $i++;
  144. }
  145. //set default roles
  146. $sv = pnModFunc('iw_main', 'user', 'genSecurityValue');
  147. pnModFunc('iw_main', 'user', 'userSetVar', array('uid' => $uid,
  148. 'name' => 'defaultRoles',
  149. 'module' => 'iw_myrole',
  150. 'sv' => $sv,
  151. 'value' => $groups));
  152. }
  153. if($setDefault == 1){
  154. $i = 0;
  155. foreach($roles as $group){
  156. $groups .= $group['id'].'$$';
  157. $i++;
  158. }
  159. //set default roles
  160. $sv = pnModFunc('iw_main', 'user', 'genSecurityValue');
  161. pnModFunc('iw_main', 'user', 'userSetVar', array('uid' => $uid,
  162. 'name' => 'defaultRoles',
  163. 'module' => 'iw_myrole',
  164. 'sv' => $sv,
  165. 'value' => $groups));
  166. }
  167. //Check if the group that can change roles have admin permisions. If not the block is not showed
  168. $correctGroupPermissions = pnModAPIFunc('iw_myrole', 'admin', 'correctGroupPermissions');
  169. if(!$correctGroupPermissions){
  170. return pnRedirect(pnModURL());
  171. }
  172. // Esborrem la pertinença a tots els grups excepte el de canvia de rol
  173. $delGroups = pnModApiFunc('iw_myrole', 'admin', 'delUserGroups');
  174. if ($delGroups) {
  175. $addToGroup = pnModApiFunc('iw_myrole', 'admin', 'addUserToGroup', array('roles' => $roles));
  176. }
  177. if(!$delGroups || !$addToGroup){
  178. LogUtil::registerError (__('Error in the role change', $dom));
  179. }
  180. pnModFunc('iw_main', 'user', 'regenBlockNews', array('sv' => $sv));
  181. return true;
  182. }
  183. /**
  184. * Reset user groups membership
  185. * @author: Josep Ferràndiz (jferran6@xtec.cat)
  186. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  187. * @param: none
  188. * @return: True if success or false otherwise
  189. */
  190. function iw_myrole_admin_resetRoles($args)
  191. {
  192. $dom = ZLanguage::getModuleDomain('iw_myrole');
  193. // Security check
  194. if (!SecurityUtil::checkPermission('iw_myrole::', "::", ACCESS_ADMIN)) {
  195. return LogUtil::registerError(__('Sorry! No authorization to access this module.', $dom), 403);
  196. }
  197. //get the headlines saved in the user vars. It is renovate every 10 minutes
  198. $sv = pnModFunc('iw_main', 'user', 'genSecurityValue');
  199. $exists = pnModApiFunc('iw_main', 'user', 'userVarExists', array('name' => 'defaultRoles',
  200. 'module' => 'iw_myrole',
  201. 'uid' => pnUserGetVar('uid'),
  202. 'sv' => $sv));
  203. if(!$exists){
  204. $sv = pnModFunc('iw_main', 'user', 'genSecurityValue');
  205. pnModFunc('iw_main', 'user', 'userSetVar', array('uid' => pnUserGetVar('uid'),
  206. 'name' => 'invalidChange',
  207. 'module' => 'iw_myrole',
  208. 'lifetime' => 10,
  209. 'nult' => true,
  210. 'value' => 1,
  211. 'sv' => $sv));
  212. return pnRedirect($_SERVER['HTTP_REFERER']);
  213. }
  214. // Esborrem la pertinença a tots els grups excepte el de canvia de rol
  215. $delGroups = pnModApiFunc('iw_myrole', 'admin', 'delUserGroups');
  216. // Esborrem la pertinença a tots els grups excepte el de canvia de rol
  217. pnModAPIFunc('iw_myrole', 'admin', 'addUserToGroup', array('defaultRoles' => 1));
  218. pnModFunc('iw_main', 'user', 'regenBlockNews', array('sv' => $sv));
  219. return true;
  220. }