Page 1 of 1

Switching the rolle in AppGini Application

Posted: 2018-10-06 08:42
by pbottcher
Hi,

as there are multiple reuqest to handle multipe group permission (like requested in post)

https://forums.appgini.com/phpbb/viewto ... f=6&t=2799

I would like to provide an interim possibility to handle the switching of a user between multiple groups.

First you create a groups table in AppGini with
AppGini_Groups.JPG
user as varchar (size as you need for your user, should be at least 20 as this is the default in AppGini for the MemberID).
groupname as varchar (size as you need for your user, should be at least 20 as this is the default in AppGini for the GroupName).

Next you generate you application.

In the hooks/groups.php file you modify the groups_init function to:

Code: Select all

	function groups_init(&$options, $memberInfo, &$args){
 
	$users=sqlvalue("SELECT GROUP_CONCAT(memberID SEPARATOR ';;') FROM membership_users GROUP BY NULL");
	file_put_contents("hooks/groups.user.csv", print_r($users, TRUE));
	$groups=sqlvalue("SELECT GROUP_CONCAT(name SEPARATOR ';;') FROM membership_groups GROUP BY NULL");
	file_put_contents("hooks/groups.groupname.csv", print_r($groups, TRUE));
		return TRUE;
	}
in the hooks/header-extras.php you add

Code: Select all

<?php

 if(!defined('PREPEND_PATH')) define('PREPEND_PATH', ''); 
	$user=getLoggedMemberID();
	$memberinfo=getMemberInfo();
	$maingroup=$memberinfo["group"];

	if ($user == "guest") return;

	$query = "SELECT groupname from groups where user='".$user."' and groupname != '".$maingroup."'";

	$result = sql($query,$eo);
	$output="'";

	$count = $result->num_rows;
	if ($count > 0 ) {
		while($row = db_fetch_assoc($result)) {
			$output .= "<li><a href=\"change_group.php?User=".$user."&Group=".$row['groupname']."\">	<small><em>".$row['groupname']."</em></small></a></li>";
		}
	}
	else { 
			$output .= "<li> <small>No additional groups available</small>	</a> </li>";
	}
	$output .="'";
?>

<script type="text/javascript">
var list=<?php echo $output; ?>;
$j('nav .navbar-collapse').append( '<ul class="nav navbar-nav">' + 
								'<li class="dropdown">' + 
									'<a href="#" class="dropdown-toggle" data-toggle="dropdown" id="User_Group"><span id="User_Group-count" class="label label-pill label-danger count"  style="border-radius:10px;"><?php echo " ".$count; ?></span> <span> <i class="glyphicon glyphicon-user" style="font-size:18px;"></i></span><?php echo " ".$maingroup; ?></a>' + 
									'<ul class="dropdown-menu" role=menu id="User_Group-text"></ul>' + 
								'</li>' +
							'</ul>' ); 
					$j('#User_Group-text').html(list);

</script>
Next you create a file in you main app directory named change_group.php with:

Code: Select all

<?php
	$curr_dir = dirname(__FILE__);
	include("{$curr_dir}/defaultLang.php");
	include("{$curr_dir}/language.php");
	include("{$curr_dir}/lib.php");
	$group = $_REQUEST['Group'];
	
	sqlvalue("UPDATE membership_users set groupID=(select groupID from membership_groups where name = '".$group."')");

header("Location: ".$_SERVER['HTTP_REFERER']);
Now you need to allow access to this group table to those people that will handle the group assignment for the users.

Hope that helps

Re: Switching the rolle in AppGini Application

Posted: 2018-10-11 14:03
by a.gneady
Very interesting. Thanks for sharing this tip ^_^

Re: Switching the rolle in AppGini Application

Posted: 2018-10-17 06:49
by fbrano
After I test to switch user group privilegs acording switched group did not changed. After all when I log out I got this error...

( ! ) Fatal error: Maximum function nesting level of '256' reached, aborting! in C:\wamp\www\elp\settings-manager.php on line 111
Call Stack
# Time Memory Function Location
1 0.0070 138360 {main}( ) ...\index.php:0
2 0.1980 1186872 getTableList( ) ...\index.php:11
3 0.1980 1190880 getTablePermissions( ) ...\incCommon.php:58
4 0.1980 1190920 getLoggedMemberID( ) ...\incCommon.php:92
5 0.1990 1191040 getLoggedMemberID( ) ...\incFunctions.php:1247
...
253 0.4220 1256536 getLoggedMemberID( ) ...\incFunctions.php:1247
254 0.4220 1256536 setAnonymousAccess( ) ...\incFunctions.php:1246
255 0.4230 1256968 sql( ) ...\incFunctions.php:1258
256 0.4230 1257024 config( ) ...\incFunctions.php:294

Re: Switching the rolle in AppGini Application

Posted: 2018-10-17 08:43
by fbrano
Reason of crussing application is in modification values in table membership_users groupID

Re: Switching the rolle in AppGini Application

Posted: 2018-10-17 09:45
by fbrano
This code solved modification only curent user's group:

<?php
$curr_dir = dirname(__FILE__);
include("{$curr_dir}/defaultLang.php");
include("{$curr_dir}/language.php");
include("{$curr_dir}/lib.php");
$group = $_REQUEST['Group'];
$memberinfo = getMemberInfo();

sqlvalue("UPDATE membership_users set groupID=(select groupID from membership_groups where name = '{$group}') where memberID = '{$memberinfo['username']}'");

header("Location: ".$_SERVER['HTTP_REFERER']);

Still user have to logout and login to be able to use credentials of changed group. Is it able to activate credentials of switched group automaticaly without logout and login?

Re: Switching the rolle in AppGini Application

Posted: 2018-10-18 10:46
by pbottcher
Hi,

thanks for testing and highlighting the error.

The change_group.php should indeed look like

<?php
$curr_dir = dirname(__FILE__);
include("{$curr_dir}/defaultLang.php");
include("{$curr_dir}/language.php");
include("{$curr_dir}/lib.php");

$group = $_REQUEST['Group'];
$user = $_REQUEST['User'];
$groupid=sqlvalue("select groupID from membership_groups where name = '".$group."'");
sqlvalue("UPDATE membership_users set groupID='".$groupid."' where memberID = '".$user."'");
$_SESSION['memberGroupID']=$groupid;

header("Location: ".$_SERVER['HTTP_REFERER']);

with this adjustement you should also get a change within your active session without having to logout.

Also I saw that in the original post the image was missing for the AppGini table groups.
Here is the image.
AppGini User Table.JPG
AppGini User Table.JPG (10.89 KiB) Viewed 9414 times
Note that you should define the user and groupname as option list with any dummy entry in the listdefinition (This will be overwritten afterwards).
Groupname option list.JPG
Groupname option list.JPG (65.54 KiB) Viewed 9414 times
Hope that helps

Re: Switching the rolle in AppGini Application

Posted: 2020-08-17 11:37
by fbrano
My generated csv file with list of users ended up at the first line with 1024 characters. From all possible users I got to choose only from fraction of them... any idea why?