Switching the rolle in AppGini Application

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 296
Joined: 2018-04-01 10:12

Switching the rolle in AppGini Application

Post by pböttcher » 2018-10-06 08:42

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

User avatar
a.gneady
Site Admin
Posts: 938
Joined: 2012-09-27 14:46
Contact:

Re: Switching the rolle in AppGini Application

Post by a.gneady » 2018-10-11 14:03

Very interesting. Thanks for sharing this tip ^_^
AppGini: Responsive, collaborative web db apps in minutes.
:idea: Learn all the tips and tricks of customizing AppGini apps through our online course.

fbrano
Posts: 28
Joined: 2018-03-19 10:39

Re: Switching the rolle in AppGini Application

Post by fbrano » 2018-10-17 06:49

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

fbrano
Posts: 28
Joined: 2018-03-19 10:39

Re: Switching the rolle in AppGini Application

Post by fbrano » 2018-10-17 08:43

Reason of crussing application is in modification values in table membership_users groupID
Attachments
user switch error.JPG
user switch error.JPG (173.97 KiB) Viewed 453 times

fbrano
Posts: 28
Joined: 2018-03-19 10:39

Re: Switching the rolle in AppGini Application

Post by fbrano » 2018-10-17 09:45

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?

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 296
Joined: 2018-04-01 10:12

Re: Switching the rolle in AppGini Application

Post by pböttcher » 2018-10-18 10:46

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 431 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 431 times
Hope that helps

Post Reply