Title1

Title2

Title3

9-5-2 權限管理

一、admin/menu.php
由於後台我打算只用來設定權限,故使用ADMENU1,語系檔則要去修改

二、修改後台選單

  1. admin/menu.php
    
    $i++;
    $adminmenu[$i]['title'] =_MI_TNCUCART_ADMENU1;
    $adminmenu[$i]['link'] = 'admin/groupperm.php';
    $adminmenu[$i]['desc'] = _MI_TNCUCART_ADMENU1_DESC;
    $adminmenu[$i]['icon'] = 'images/admin/button.png';

     

  2. 修改語系
    language/tchinese_utf8/modinfo.php
    
    define("_MI_TNCUCART_ADMENU1", "權限管理");
    define("_MI_TNCUCART_ADMENU1_DESC", "後台權限管理");

     

  3. admin/groupperm.php
    <?php
    /*-----------引入檔案區--------------*/
    include_once "header.php";
    include_once "../function.php";
    //引入權限設定語系
    include_once XOOPS_ROOT_PATH."/modules/ugm_tools2/language/{$xoopsConfig['language']}/groupperm.php";
    //群組選擇使用
    include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
    
    # ---- 模組名稱 ----
    $DIRNAME=$xoopsModule->getVar('dirname');
    # ---- 程式名稱(含副檔名) ----
    $file_name = basename(__FILE__);
    //取得本模組編號
    $gperm_modid = $xoopsModule->getVar('mid');
    
    #引入權限檔
    require_once XOOPS_ROOT_PATH . "/modules/{$DIRNAME}/groupperm.php";
    
    /*-----------執行動作判斷區----------*/
    $op=empty($_REQUEST['op'])?'':$_REQUEST['op'];
    
    if($op=="op_insert"){
    	op_insert();
    	redirect_header($_SERVER['PHP_SELF'],3,_MA_PERM_SUCCESS);
    }
    
    $main="
    <!-- groupperm begin -->
    
    <link rel='stylesheet' type='text/css' media='screen' href='".XOOPS_URL."/modules/tadtools/bootstrap3/css/bootstrap.css'>
    <link rel='stylesheet' type='text/css' media='screen' href='".XOOPS_URL."/modules/ugm_tools2/css/xoops_adm3.css'>
    <link rel='stylesheet' type='text/css' media='screen' href='".XOOPS_URL."/modules/ugm_tools2/css/module_b3.css'>
    
    <div class='CPbigTitle' style='background-image: url(".XOOPS_URL."/modules/ugm_tools2/images/admin/button.png); background-repeat: no-repeat; background-position: left; padding-left: 50px;''><strong>"._MA_GROUPPERM_TITLE."</strong>
    </div>
    <br />
    
    <style>
    	#form_table th{text-align: center;}
    	#form_table td{padding:20px 10px;}
    </style>
    
    <div id='save_msg'></div>
    
    <div>
    	<a class='tooltip' href='".XOOPS_URL."/modules/system/admin.php?fct=groups' title='"._MA_GROUPPERM_GROUP."'>
    		<img src='".XOOPS_URL."/modules/system/themes/default/icons/groups.png' alt='' title=''>
    	</a>
    	<a class='tooltip' href='".XOOPS_URL."/modules/system/admin.php?fct=users' title='"._MEMBERS."'>
    		<img src='".XOOPS_URL."/modules/system/themes/default/icons/edituser.png' alt='' title=''>
    	</a>
    </div>
    
    <form action='{$file_name}' method='post' id='myForm' class='form-inline'>
    	<table id='form_table' class='table table-bordered table-hover table-condensed'>
    		<!--標題-->
    		<thead>
    			<tr>
    				<th></th>";
    
    
    foreach($gperm_itemid_arr as $x){
    	$main.="
    		<th>{$x['title']}</th>";
    }
    
    $main.="
    			</tr>
    		</thead>";
    foreach($gperm_name_arr as $gperm_name=>$gperm_name_title){
    	$main.="
    			<tr>
    				<td>{$gperm_name_title}</td>";
    
    	foreach($gperm_itemid_arr as $gperm_itemid=>$x){
    		#預設值(陣列)get_gpermDB_arr($modules_id,$gperm_name,$gperm_itemid)
    		$gpermDB_arr=get_gpermDB_arr($gperm_modid,$gperm_name,$gperm_itemid);
    		//
    		# ---- XoopsFormSelectGroup('標題', 'form_name',訪客權限, '預設值(陣列)', 高度 ,多選)
    		$SelectGroup_name = new XoopsFormSelectGroup("", "gperm[{$gperm_name}][{$gperm_itemid}]", false,$gpermDB_arr, 4, true);
    		if($x['anonymous']){
    			$SelectGroup_name->addOption(0, _MA_GROUPPERM_ALL, false);
    		}
    		$SelectGroup_name->setExtra("class='col-md-12'");
    		$main.="<td>".$SelectGroup_name->render()."</td>";
    	}
    	$main.="</tr>";
    }
    
    $main.="
    		<tfoot>
    			<tr>
    			<td colspan=6 class='text-center'>
    			<input type='hidden' name='op' value='op_insert'>
    			<input type='hidden' name='gperm_modid' value='{$gperm_modid}'>
    ";
    
    foreach($gperm_name_arr as $gperm_name=>$v){
    	$main.="
    	<input type='hidden' name='gperm_name[]' value='{$gperm_name}'>";
    }
    
    $main.="
    					<button type='submit' class='btn btn-primary'>"._SUBMIT."</button>
    				</td>
    			</tr>
    		</tfoot>
    	</table>
    </form>
    
    <!-- groupperm end -->
    ";
    
    echo $main;
    include_once "footer.php";
    /*-----------秀出結果區--------------*/
    
    #insert
    function op_insert(){
    	global $xoopsDB;
    	//---- 過濾資料 -----------------------------------------
    	$myts =& MyTextSanitizer::getInstance();
    	$gperm_modid=intval($_POST['gperm_modid']);//模組編號
    
    	//---- 刪除設定權限資料 $gperm_modid
    	foreach ($_POST['gperm_name'] as $gperm_name){
    		//權限名稱
    		$gperm_name=$myts->addSlashes($gperm_name);
    		$sql = "delete from ".$xoopsDB->prefix("group_permission")."
    		where gperm_modid='{$gperm_modid}' and gperm_name='{$gperm_name}' ";//die($sql);
    		$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, web_error());
    	}
    
    	//---- 寫入選擇的權限設定
    	//gperm[ugmmrp_customer][1][]
    	foreach ($_POST['gperm'] as $gperm_name => $gperm_itemid_arr){
    		$gperm_name=$myts->addSlashes($gperm_name);//權限名稱
    		foreach ($gperm_itemid_arr as $gperm_itemid => $gperm_groupid_arr){
    			$gperm_itemid=intval($gperm_itemid);//權限項目編號
    			foreach ($gperm_groupid_arr as $gperm_groupid){
    				$gperm_groupid=intval($gperm_groupid);//群組編號
    
    				#---------寫入-------------------------
    				#gperm_id gperm_groupid gperm_itemid gperm_modid gperm_name
    				$sql = "insert into 
    							 ".$xoopsDB->prefix("group_permission")."
    							 (`gperm_groupid` ,`gperm_itemid` , `gperm_modid` , `gperm_name`)
    							 values
    							 ('{$gperm_groupid}' , '{$gperm_itemid}' ,'{$gperm_modid}' , '{$gperm_name}' )";
    				$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, web_error());
    			}
    		}
    	}
    	return;
    }
    
    #取得資料表中的設定("模組 id",$gperm_name,$gperm_itemid)
    #返回陣列 [0]=>群組id [1]=>=>群組id
    function get_gpermDB_arr($gperm_modid,$gperm_name,$gperm_itemid){
    	global $xoopsDB;
    
    	$sql="select gperm_groupid
    		from ".$xoopsDB->prefix("group_permission")."
    		where gperm_modid='{$gperm_modid}' and gperm_name='{$gperm_name}' and gperm_itemid='{$gperm_itemid}'
    		order by gperm_groupid
    	";//die($sql);
    	$result=$xoopsDB->query($sql);
    	$gperm_groupids="";
    	while(list($gperm_groupid)=$xoopsDB->fetchRow($result)){
    	$gperm_groupids[]=$gperm_groupid;
    	}
    	return $gperm_groupids;
    }

     

  4. 前台權限設定
    ugm_college/groupperm.php
    <?php
    /*
    此檔只引入權限函數與x、y軸,權限則由使用程式處理
    */
    defined('XOOPS_ROOT_PATH') || die("XOOPS root path not defined");
    #引入權限函數
    include_once XOOPS_ROOT_PATH . "/modules/ugm_tools2/function_gperm.php";
    
    //權限項目陣列(x軸)(編號超級重要!設定後,以後切勿隨便亂改。)
    # title 只有在後台設定時用到
    $gperm_itemid_arr = array(
      '1' => array("title"=>_ADD,"anonymous"=>false),//新增
      '2' => array("title"=>_EDIT,"anonymous"=>false),//編輯
      '3' => array("title"=>_DELETE,"anonymous"=>false),//刪除
      '4' => array("title"=>_MA_GROUPPERM_VIEW,"anonymous"=>false),//瀏覽 true
      '5' => array("title"=>_MA_GROUPPERM_PRINT,"anonymous"=>false)//列印
    );
    
    //權限名稱陣列(y軸)
    $gperm_name_arr = array(
      "tncu_cartAdmin" => _MD_TNCUCART_ADMIN
    );
    

     

  5. 語系
    language/tchinese_utf8/main.php
    language/english/main.php
    
    #permissions.php
    define("_MD_TNCUCART_ADMIN", "社大購物車管理員");

     

  6.