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: 254
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: 912
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.

Post Reply