users
[ class tree: users ] [ index: users ] [ all elements ]

Source for file my_roles.php

Documentation is available at my_roles.php

  1. <?php
  2. /**
  3.  * my roles
  4.  *
  5.  * @copyright (c) 2004-15 bitweaver.org
  6.  *
  7.  * @package users
  8.  * @subpackage functions
  9.  */
  10.  
  11. /**
  12.  * required setup
  13.  */
  14. require_once( '../kernel/setup_inc.php' );
  15.  
  16. global $gBitUser, $gBitSystem;
  17.  
  18. // PERMISSIONS: registered user required
  19. if ( !$gBitUser->isRegistered() ) {
  20.     $gBitSystem->fatalError( tra( "You are not logged in." ));
  21. }
  22.  
  23. if( !empty( $_REQUEST["cancel"] ) ) {
  24.     bit_redirect( USERS_PKG_URL.'my_roles.php' );
  25. }
  26.  
  27. if ( $gBitUser->hasPermission('p_users_create_personal_roles' ) ) {
  28.     if( !empty( $_REQUEST['role_id'] ) ) {
  29.         $allPerms = $gBitUser->getRolePermissions( array( 'sort_mode' => !empty( $_REQUEST['sort_mode'] ) ? $_REQUEST['sort_mode'] : NULL ));
  30.         // get rolelist separately from the $users stuff to avoid splitting of data due to pagination
  31.         $listHash = array( 'sort_mode' => 'role_name_asc' );
  32.         $roleList = $gBitUser->getAllRoles( $listHash );
  33.     } else {
  34.         // get rolelist separately from the $users stuff to avoid splitting of data due to pagination
  35.         $listHash = array( 'sort_mode' => !empty( $_REQUEST['sort_mode'] ) ? $_REQUEST['sort_mode'] : 'role_name_asc' );
  36.         $roleList = $gBitUser->getAllRoles( $listHash );
  37.     }
  38.     
  39.     // Remember a package limit if it is set.
  40.     $gBitSmarty->assign( 'package',isset( $_REQUEST['package'] ) ? $_REQUEST['package'] : 'all' );
  41.  
  42.     // Save the join
  43.     if( isset($_REQUEST["save"] ) ) {
  44.         if( empty($_REQUEST["name"] ) ) {
  45.             $_REQUEST["name"] = $_REQUEST["olrole"];
  46.         }
  47.         if( $gBitUser->storeRole( $_REQUEST ) ) {
  48.             $successMsg = tra("Role changes were saved sucessfully.");
  49.         } else {
  50.             $errorMsg = $gBitUser->mErrors['roles'];
  51.         }
  52.     // Update Permissions
  53.     } elseif (isset($_REQUEST['updateperms'])) {
  54.         $listHash = array( 'role_id' => $_REQUEST['role_id'] );
  55.         $updatePerms = $gBitUser->getrolePermissions( $listHash );
  56.         foreach (array_keys($_REQUEST['perm']) as $per) {
  57.             if( isset($_REQUEST['perm'][$per]) && !isset($updatePerms[$per]) ) {
  58.                 // we have an unselected perm that is now selected
  59.                 $gBitUser->assignPermissionToRole($per, $_REQUEST['role_id']);
  60.             } elseif( empty($_REQUEST['perm'][$per]) && isset($updatePerms[$per]) ) {
  61.                 // we have a selected perm that is now UNselected
  62.                 $gBitUser->removePermissionFromRole($per, $_REQUEST['role_id']);
  63.             }
  64.         }
  65.         // let's reload just to be safe.
  66.         $allPerms = $gBitUser->getRolePermissions();
  67.     // Do some action
  68.     } elseif (isset($_REQUEST["action"])) {
  69.         // Process a form to remove a role
  70.         if( $_REQUEST["action"] == 'delete' ) {
  71.             if( $gBitUser->getDefaultRole( $_REQUEST['role_id'] ) ) {
  72.                 $errorMsg = tra("You cannot remove this role, as it is currently set as your 'Default' role");
  73.             } else {
  74.                 $gBitUser->expungeRole( $_REQUEST['role_id'] );
  75.                 $successMsg = tra("The role was deleted.");
  76.                 unset( $_REQUEST['role_id'] );
  77.             }
  78.         // remove a permission from a role
  79.         } elseif ($_REQUEST["action"] == 'remove') {
  80.             $gBitUser->removePermissionFromRole( $_REQUEST["permission"], $_REQUEST['role_id'] );
  81.             $successMsg = tra("Permission Removed");
  82.             $mid = 'bitpackage:users/my_role_edit.tpl';
  83.         // Create a new role
  84.         } elseif( $_REQUEST["action"] == 'create' ) {
  85.             $gBitSystem->setBrowserTitle( tra('Create New Role') );
  86.             $mid = 'bitpackage:users/my_role_edit.tpl';
  87.         // Assign a permission to a role
  88.         } elseif ($_REQUEST["action"] == 'assign') {
  89.             $gBitUser->assignPermissionToRole($_REQUEST["perm"], $_REQUEST['role_id']);
  90.             $successMsg = tra("Permission Assigned");
  91.             $mid = 'bitpackage:users/my_role_edit.tpl';
  92.         }
  93.     // Search for users to add
  94.     } elseif (!empty($_REQUEST['submitUserSearch'])) {
  95.         $searchParams = array('find' => $_REQUEST['find']);
  96.         $gBitUser->getList($searchParams);
  97.         $foundUsers = $searchParams['data'];
  98.         $mid = 'bitpackage:users/my_role_edit.tpl';
  99.         $gBitSmarty->assign_by_ref('foundUsers', $foundUsers);
  100.     } elseif (!empty($_REQUEST['assignuser'])) {
  101.         if( !empty($_REQUEST['role_id'] ) ) {
  102.             if ($_REQUEST['role_id'] != -1 && $roleList[$_REQUEST['role_id']]['user_id'] == $gBitUser->mUserId) {
  103.                 $gBitUser->addUserToRole( $_REQUEST['assignuser'], $_REQUEST['role_id'] );
  104.             }
  105.             else {
  106.                 $errorMsg = tra("You can not assign users to this role.");
  107.             }
  108.         }
  109.         $mid = 'bitpackage:users/my_role_edit.tpl';
  110.     }
  111.  
  112.     // get pagination url
  113.     // get rolelist separately from the $users stuff to avoid splitting of data due to pagination
  114.     $listHash = array( 'sort_mode' => 'role_name_asc' );
  115.     $roleList = $gBitUser->getAllUserRoles();
  116.     
  117.     if( !empty( $_REQUEST['role_id'] ) ) {
  118.         // we don't want our own role listed when editing
  119.         if( !empty( $roleList[$_REQUEST['role_id']] ) ) {
  120.             unset( $roleList[$_REQUEST['role_id']] );
  121.         }
  122.         $roleInfo = $gBitUser->getRoleInfo( $_REQUEST['role_id'] );
  123.         $roleUsers = $gBitUser->getRoleUsers( $_REQUEST['role_id'] );
  124.         $gBitSmarty->assign_by_ref('roleUsers', $roleUsers);
  125.         $gBitSmarty->assign_by_ref('roleInfo', $roleInfo);
  126.         $gBitSmarty->assign_by_ref( 'allPerms', $allPerms );
  127.         $gBitSystem->setBrowserTitle( 'Admininster Role: '.$roleInfo['role_name'].' '.(isset( $_REQUEST['tab'] ) ? $_REQUEST['tab'] : '') );
  128.         $mid = 'bitpackage:users/my_role_edit.tpl';
  129.     } 
  130.  
  131.     $gBitSmarty->assign('roles', $roleList);
  132.     //    $gBitSmarty->assign( (!empty( $_REQUEST['tab'] ) ? $_REQUEST['tab'] : 'edit').'TabSelect', 'tdefault' );
  133. }
  134.  
  135. /* join or leave a public role. */
  136. if ( ( !empty( $_REQUEST['add_public_role'] ) || !empty( $_REQUEST['remove_public_role'] ) ) && !empty( $_REQUEST['public_role_id'] ) ) {
  137.     $roleInfo = $gBitUser->getRoleInfo( $_REQUEST['public_role_id'] );
  138.     if ( empty($roleInfo) || $roleInfo['is_public'] != 'y' ) {
  139.         if (empty($_REQUEST['add_public_role'])) {
  140.             $errorMsg[] = tra("You can't join this role.");
  141.         }
  142.         else {
  143.             $errorMsg[] = tra("You can't leave this role.");
  144.         }            
  145.     } elseif ( !empty( $_REQUEST['add_public_role'] ) ) {
  146.         $gBitUser->addUserToRole( $gBitUser->mUserId, $_REQUEST['public_role_id'] );
  147.     } elseif ( !empty( $_REQUEST['remove_public_role'] ) ) {
  148.         $gBitUser->removeUserFromRole( $gBitUser->mUserId, $_REQUEST['public_role_id'] );
  149.     }
  150.     $gBitUser->loadPermissions();
  151.     if ( !empty( $_REQUEST['add_public_role'] ) && !empty( $roleInfo['after_registration_page'] ) ) {
  152.         if ( $gBitUser->verifyId( $roleInfo['after_registration_page'] ) ) {
  153.             $url = BIT_ROOT_URL."index.php?content_id=".$roleInfo['after_registration_page'];
  154.         } elseif( strpos( $roleInfo['after_registration_page'], '/' ) === FALSE ) {
  155.             $url = BitPage::getDisplayUrl( $roleInfo['after_registration_page'] );
  156.         } else {
  157.             $url = $roleInfo['after_registration_page'];
  158.         }
  159.         header( 'Location: '.$url );
  160.         exit;
  161.     }
  162. }
  163.  
  164. /* Load up public roles and check if the user can join or leave them */
  165. $systemRoles = $gBitUser->getRoles( $gBitUser->mUserId, TRUE );
  166. $gBitSmarty->assign_by_ref( 'systemRoles', $systemRoles);
  167. $listHash = array(
  168.     'is_public'=>'y',
  169.     'sort_mode' => array( 'is_default_asc', 'role_desc_asc' ),
  170. );
  171. $publicRoles = $gBitUser->getAllRoles( $listHash );    
  172. if( count( $publicRoles )) {
  173.     foreach ( $systemRoles as $roleId=>$roleInfo ) {
  174.         foreach ( $publicRoles as $key=>$publicRole) {
  175.             if ( $publicRoles[$key]['role_id'] == $roleId) {
  176.                 if ($publicRoles[$key]['is_default'] != 'y' ) {
  177.                     $systemRoles[$roleId]['public'] = 'y';
  178.                     $canRemovePublic = 'y';
  179.                 }
  180.                 $publicRoles[$key]['used'] = 'y';
  181.                 break;
  182.             }
  183.         }
  184.     }
  185.     foreach ( $publicRoles as $roleInfo) {
  186.         if ( empty($roleInfo['used'] ) && $roleInfo['is_default'] != 'y' ) {
  187.             $gBitSmarty->assign( 'canAddPublic' , 'y');
  188.             break;
  189.         }
  190.     }
  191.     $gBitSmarty->assign_by_ref( 'publicRoles', $publicRoles );
  192.     if (isset($canRemovePublic)) {
  193.         $gBitSmarty->assign( 'canRemovePublic' , 'y');
  194.     }
  195. }
  196.  
  197. // Remember error and success messages.    
  198. if (!empty($errorMsg)) {
  199.     $gBitSmarty->assign('errorMsg',$errorMsg);
  200. }
  201. if (!empty($successMsg)) {
  202.     $gBitSmarty->assign('successMsg',$successMsg);
  203. }
  204.  
  205. // Default the template if we aren't doing an edit.
  206. if (empty($mid)) {
  207.     $mid = 'bitpackage:users/my_roles_list.tpl';
  208. }
  209.  
  210. // Display the template for role administration
  211. $gBitSystem->display( $mid , NULL, array( 'display_mode' => 'display' ));
  212. ?>

Documentation generated on Wed, 29 Jul 2015 13:57:14 +0000 by phpDocumentor 1.5.0-lsces