permissions page - if admin check a permission father its children will be hidden

This commit is contained in:
Daniel Camargo 2013-11-01 15:46:34 -02:00
parent d7a9382df4
commit 0112361eec
5 changed files with 245 additions and 41 deletions

View File

@ -112,12 +112,17 @@ class Crunchbutton_Admin extends Cana_Table {
return $this->_permissions;
}
public function hasPermission( $permission ){
public function hasPermission( $permission, $useRegex = false ){
$permissions = $this->permissions();
foreach( $permissions as $_permission ){
if( $_permission->permission == $permission && $_permission->allow == 1 ){
return true;
}
if( $useRegex ){
if( preg_match( $permission, $_permission->permission ) && $_permission->allow == 1 ){
return true;
}
}
}
return false;
}
@ -134,6 +139,8 @@ class Crunchbutton_Admin extends Cana_Table {
$_permission->permission = trim( $key );
$_permission->allow = 1;
$_permission->save();
// reset the permissions
$this->_permissions = false;
$dependencies = $_permission->getDependency( $key );
if( $dependencies ){
foreach( $dependencies as $dependency ){

View File

@ -19,19 +19,32 @@ class Crunchbutton_Admin_Permission extends Cana_Table {
/* Restaurants's permissions */
$_permissions[ 'restaurant' ] = array( 'description' => 'Restaurant\'s permissions' );
$_permissions[ 'restaurant' ][ 'doAllPermission' ] = 'restaurants-all';
$_permissions[ 'restaurant' ][ 'permissions' ] = array(
'restaurants-all' => array( 'description' => 'Can perform any action with ALL restaurants' ),
'restaurants-list-page' => array( 'description' => 'View restaurants he has access to' ),
'restaurants-crud' => array( 'description' => 'Create, update, retrieve and delete ALL restaurants' ),
'restaurant-ID-all' => array( 'description' => 'Create, update, retrieve and delete ONLY these restaurants', 'type' => 'combo', 'element' => 'Restaurant', 'dependency' => array( 'restaurants-list-page' ) ),
'restaurant-ID-edit' => array( 'description' => 'Edit the info about the restaurant ID, it does not include payment and send fax', 'type' => 'combo', 'element' => 'Restaurant', 'dependency' => array( 'restaurants-list-page' ) ),
'restaurant-ID-pay' => array( 'description' => 'Make the payment of the restaurant ID', 'type' => 'combo', 'element' => 'Restaurant', 'dependency' => array( 'restaurants-list-page' ) ),
'restaurant-ID-fax' => array( 'description' => 'Send fax to the restaurant ID', 'type' => 'combo', 'element' => 'Restaurant', 'dependency' => array( 'restaurants-list-page' ) ),
'restaurants-weight-adj-page' => array( 'description' => 'Adjust weight (user can edit just restaurants he has access to)', 'dependency' => array( 'restaurants-list-page' ) ),
'restaurant-ID-edit' => array(
'description' => 'Edit the info about the restaurant ID, it does not include payment and send fax',
'type' => 'combo',
'element' => 'Restaurant',
'dependency' => array( 'restaurants-list-page' ),
'additional' => array(
'label' => 'Additional restaurant permissions:',
'permissions' => array(
'restaurants-weight-adj-page' => array( 'description' => 'View the adjustment weight page, the user will be able to edit just the restaurant he has the permission', 'dependency' => array( 'restaurants-list-page' ) ),
'restaurant-ID-pay' => array( 'description' => 'Payment', 'dependency' => array( 'restaurants-list-page' ) ),
'restaurant-ID-fax' => array( 'description' => 'Fax', 'dependency' => array( 'restaurants-list-page' ) ),
)
)
),
);
/* Orders's permissions */
$_permissions[ 'order' ] = array( 'description' => 'Orders\'s permissions' );
$_permissions[ 'order' ][ 'doAllPermission' ] = 'orders-all';
$_permissions[ 'order' ][ 'permissions' ] = array(
'orders-all' => array( 'description' => 'Can perform any action with orders' ),
'orders-list-page' => array( 'description' => 'View new users page for restaurants he has access to' ),
@ -44,6 +57,7 @@ class Crunchbutton_Admin_Permission extends Cana_Table {
/* Gift card's permissions */
$_permissions[ 'giftcard' ] = array( 'description' => 'Gift card\'s permissions' );
$_permissions[ 'giftcard' ][ 'doAllPermission' ] = 'gift-card-all';
$_permissions[ 'giftcard' ][ 'permissions' ] = array(
'gift-card-all' => array( 'description' => 'Can perform any action with gift cards' ),
'gift-card-list-page' => array( 'description' => 'View main gift card page' ),
@ -60,6 +74,7 @@ class Crunchbutton_Admin_Permission extends Cana_Table {
/* Metric's permissions */
$_permissions[ 'metrics' ] = array( 'description' => 'Metric\'s permissions' );
$_permissions[ 'metrics' ][ 'doAllPermission' ] = 'metrics-all';
$_permissions[ 'metrics' ][ 'permissions' ] = array(
'metrics-all' => array( 'description' => 'View all metrics' ),
'metrics-main' => array( 'description' => 'View the `Main` charts' ),
@ -74,6 +89,7 @@ class Crunchbutton_Admin_Permission extends Cana_Table {
/* Support's permissions */
$_permissions[ 'support' ] = array( 'description' => 'Support\'s permissions' );
$_permissions[ 'support' ][ 'doAllPermission' ] = 'support-all';
$_permissions[ 'support' ][ 'permissions' ] = array(
'support-all' => array( 'description' => 'Can perform ALL support related actions' ),
'support-crud' => array( 'description' => 'Create, update and delete any support ticket' ),
@ -83,6 +99,7 @@ class Crunchbutton_Admin_Permission extends Cana_Table {
/* Suggestions's permissions */
$_permissions[ 'suggestion' ] = array( 'description' => 'Suggestions\'s permissions' );
$_permissions[ 'suggestion' ][ 'doAllPermission' ] = 'suggestions-all';
$_permissions[ 'suggestion' ][ 'permissions' ] = array(
'suggestions-all' => array( 'description' => 'Can perform any action with suggestions' ),
'suggestions-list-page' => array( 'description' => 'View suggestions page' ),
@ -91,6 +108,7 @@ class Crunchbutton_Admin_Permission extends Cana_Table {
/* Other's permissions */
$_permissions[ 'permissions' ] = array( 'description' => 'Admin user\'s permissions' );
$_permissions[ 'permissions' ][ 'doAllPermission' ] = 'permission-all';
$_permissions[ 'permissions' ][ 'permissions' ] = array(
'permission-all' => array( 'description' => 'Can perform ALL actions with admin users and groups (i.e. create, update, delete, assign permissions)' ),
'permission-users' => array( 'description' => 'Can perform actions with ONLY admin users (create, update, delete, assign permissions) ' ),
@ -130,11 +148,21 @@ class Crunchbutton_Admin_Permission extends Cana_Table {
$all_permissions = $this->_permissions;
foreach( $all_permissions as $group ){
$permissions = $group[ 'permissions' ];
foreach( $permissions as $key => $val ){
foreach( $permissions as $key => $meta ){
$regex = str_replace( 'ID' , '(.)*', $key );
$regex = '/' . $regex . '/';
if( preg_match( $regex, $permission ) > 0 ){
return $val;
return $meta;
}
if( $meta[ 'additional' ] ){
$additional_permissions = $meta[ 'additional' ][ 'permissions' ];
foreach( $additional_permissions as $_key => $_meta ){
$regex = str_replace( 'ID' , '(.)*', $_key );
$regex = '/' . $regex . '/';
if( preg_match( $regex, $permission ) > 0 ){
return $_meta;
}
}
}
}
}

View File

@ -17,16 +17,22 @@ class Crunchbutton_Group extends Cana_Table {
return $this->_permissions;
}
public function hasPermission( $permission ){
public function hasPermission( $permission, $useRegex = false ){
$permissions = $this->permissions();
foreach( $permissions as $_permission ){
if( $_permission->permission == $permission && $_permission->allow == 1 ){
return true;
}
if( $useRegex ){
if( preg_match( $permission, $_permission->permission ) && $_permission->allow == 1 ){
return true;
}
}
}
return false;
}
public static function find($search = []) {
$query = 'SELECT `group`.* FROM `group` WHERE id_group IS NOT NULL ';
@ -60,13 +66,14 @@ class Crunchbutton_Group extends Cana_Table {
$_permission->permission = trim( $key );
$_permission->allow = 1;
$_permission->save();
// reset the permissions
$this->_permissions = false;
$dependencies = $_permission->getDependency( $key );
if( $dependencies ){
foreach( $dependencies as $dependency ){
$this->addPermissions( array( $dependency => 1 ) );
}
}
}
}
}

View File

@ -1,16 +1,16 @@
<?
$this->title = 'Permissions';
$this->titleicon = 'lock';
$this->titleLink = '/permissions/groups';
$this->title = 'Permissions';
$this->titleicon = 'lock';
$this->titleLink = '/permissions/groups';
$this->title2 = 'Groups permissions';
$this->title2icon = 'group';
$this->title2 = 'Groups permissions';
$this->title2icon = 'group';
$group = $this->group;
$_permissions = $this->permissions;
$_elements = $this->elements;
$group = $this->group;
$_permissions = $this->permissions;
$_elements = $this->elements;
?>
?>
<div class="container-fluid padded">
<div class="row-fluid">
<div class="span6">
@ -18,11 +18,12 @@
Permissions of Group <?php echo $group->name ?>
</h4>
<?php
foreach( $_permissions as $permission_group ){
foreach( $_permissions as $_group => $permission_group ){
$description = $permission_group[ 'description' ];
$permissions = $permission_group[ 'permissions' ];
$doAllPermission = $permission_group[ 'doAllPermission' ];
?>
<div class="box">
<div class="box box-<?php echo $_group; ?> permission-box">
<div class="box-header">
<span class="title">
<?php echo $description; ?>
@ -46,14 +47,18 @@
$type = 'checkbox';
break;
};
$isPermissionFather = false;
if( $doAllPermission ){
$isPermissionFather = ( $doAllPermission == $permission );
}
?>
<li>
<li class="permission-row row-<?php echo $permission; ?> <?php if( $isPermissionFather ) { ?>row-father-<?php echo $doAllPermission;?><?php } else { ?>row-child-<?php echo $doAllPermission;?><?php } ?>">
<label for="<?php echo $permission; ?>">
<?php if( $type == 'checkbox' ) {
$checked = ( $group->hasPermission( $permission ) ) ? 'checked="checked"' : '' ;
?>
<span class="pull-right">
<input type="checkbox" <?php echo $checked; ?> class="icheck permissions" value="1" name="<?php echo $permission; ?>" id="<?php echo $permission; ?>">
<input type="checkbox" <?php echo $checked; ?> class="icheck permissions <?php if( $isPermissionFather ) { ?>do-all-father <?php } ?>" family-name="<?php echo $doAllPermission;?>" value="1" name="<?php echo $permission; ?>" id="<?php echo $permission; ?>">
</span>
<?php } ?>
&nbsp;<?php echo $description; ?>
@ -61,7 +66,7 @@
&nbsp;(<?php echo $permission; ?>)
</span>
<?php if( $type == 'combo' ) { ?>
<select id="<?php echo $permission; ?>" class="chzn-select permissions" multiple name="<?php echo $permission; ?>" data-placeholder="Choose" style="width:100%;">
<select id="<?php echo $permission; ?>" class="chzn-select permission-input permissions" multiple name="<?php echo $permission; ?>" data-placeholder="Choose" style="width:100%;">
<?php
$elements = $_elements[ $element ];
foreach ( $elements as $element ) {
@ -73,6 +78,32 @@
</select>
<?php } ?>
</label>
<?php if ( $meta[ 'additional' ] ) {
$adicional = $meta[ 'additional' ];
$adicional_permissions = $adicional[ 'permissions' ];
$father = $permission;
?>
<b><?php echo $adicional[ 'label' ]; ?></b><br/>
<?php
foreach( $adicional_permissions as $permission => $meta ){
$description = $meta[ 'description' ];
$pattern = '/' . str_replace( 'ID' , '.*', $permission ) . '/';
$checked = ( $group->hasPermission( $pattern, true ) ) ? 'checked="checked"' : '' ;
?>
<div class="row-fluid">
<label for="<?php echo $permission; ?>">
<span class="pull-right">
<input type="checkbox" <?php echo $checked; ?> class="icheck additional-permissions" father="<?php echo $father; ?>" value="1" name="<?php echo $permission; ?>" id="<?php echo $permission; ?>">
</span>
&nbsp;<?php echo $description; ?>
<span class="note">
&nbsp;(<?php echo $permission; ?>)
</span>
</label>
</div>
<?php
}
} ?>
</li>
<?php
}
@ -86,7 +117,7 @@
<div class="box">
<div class="box-header">
<span class="title">
Save all changes
Save
</span>
</div>
<div class="box-content ">
@ -104,11 +135,48 @@
<script type="text/javascript">
$(function() {
$(document).on('click', '.admin-save', function() {
$(document).on( 'click', '.admin-save', function() {
sendForm();
} );
$( '#global' ).on( 'ifChanged', function(event){
validateChecked( '#global' );
});
$( '.do-all-father' ).on( 'ifChanged', function( event ){
validateDoAllPermissions( $( '#' + event.target.id ) );
});
$( '.do-all-father' ).each( function( index, el ){
validateDoAllPermissions( $( el ) );
} );
validateChecked( '#global' );
});
function validateDoAllPermissions( el ){
if( el.is( ':checked' ) ){
$( '.row-child-' + el.attr( 'family-name' ) ).hide();
} else {
$( '.row-child-' + el.attr( 'family-name' ) ).show();
}
}
function validateChecked( id ){
var el = $( id );
switch( id ){
case '#global':
if( el.is( ':checked' ) ){
$( '.permission-box' ).hide();
$( '.box-global' ).show();
} else {
$( '.permission-box' ).show();
}
break;
}
}
function sendForm(){
var data = {};
@ -116,20 +184,33 @@
$('.permissions').each( function(){
var el = $( this );
var type = null;
if( el.is( 'input' ) ){
if( el.is( 'input' ) && el.is( ':visible' ) ){
type = el.attr( 'type' );
if( type == 'checkbox' ){
if( el.is( ':checked' ) ){
data[ el.attr( 'name' ) ] = 1;
}
}
} else if( el.is( 'select' ) ){
} else if( el.is( 'select' ) && el.is( ':visible' ) ){
var options = el.val();
$( options ).each( function( index, val ){
data[ val ] = 1;
} );
}
} );
$('.additional-permissions').each( function(){
var el = $( this );
if( el.is( ':checked' ) && el.is( ':visible' ) ){
var father = el.attr( 'father' );
var pattern = new RegExp( father.replace( 'ID', '((.)*)' ) ,'gm');
var values = $( '#' + father ).val();
$( values ).each( function( index, val ){
var id = val.replace( pattern, "\$1");
var permission = el.attr( 'name' ).replace( 'ID', id );
data[ permission ] = 1;
} );
}
} );
var url = App.service + 'permissions/groups/<?php echo $group->id_group; ?>/permissions';

View File

@ -19,11 +19,12 @@
"<?php echo $admin->name ?>" has these permissions in addition to his group permissions.
</h4>
<?php
foreach( $_permissions as $permission_group ){
foreach( $_permissions as $group => $permission_group ){
$description = $permission_group[ 'description' ];
$permissions = $permission_group[ 'permissions' ];
$doAllPermission = $permission_group[ 'doAllPermission' ];
?>
<div class="box">
<div class="box box-<?php echo $group; ?> permission-box">
<div class="box-header">
<span class="title">
<?php echo $description; ?>
@ -47,14 +48,18 @@
$type = 'checkbox';
break;
};
$isPermissionFather = false;
if( $doAllPermission ){
$isPermissionFather = ( $doAllPermission == $permission );
}
?>
<li>
<li class="permission-row row-<?php echo $permission; ?> <?php if( $isPermissionFather ) { ?>row-father-<?php echo $doAllPermission;?><?php } else { ?>row-child-<?php echo $doAllPermission;?><?php } ?>">
<label for="<?php echo $permission; ?>">
<?php if( $type == 'checkbox' ) {
$checked = ( $admin->hasPermission( $permission ) ) ? 'checked="checked"' : '' ;
?>
<span class="pull-right">
<input type="checkbox" <?php echo $checked; ?> class="icheck permissions" value="1" name="<?php echo $permission; ?>" id="<?php echo $permission; ?>">
<input type="checkbox" <?php echo $checked; ?> class="icheck permissions <?php if( $isPermissionFather ) { ?>do-all-father <?php } ?>" family-name="<?php echo $doAllPermission;?>" value="1" name="<?php echo $permission; ?>" id="<?php echo $permission; ?>">
</span>
<?php } ?>
&nbsp;<?php echo $description; ?>
@ -62,7 +67,7 @@
&nbsp;(<?php echo $permission; ?>)
</span>
<?php if( $type == 'combo' ) { ?>
<select id="<?php echo $permission; ?>" class="chzn-select permissions" multiple name="<?php echo $permission; ?>" data-placeholder="Choose" style="width:100%;">
<select id="<?php echo $permission; ?>" class="chzn-select permission-input permissions" multiple name="<?php echo $permission; ?>" data-placeholder="Choose" style="width:100%;">
<?php
$elements = $_elements[ $element ];
foreach ( $elements as $element ) {
@ -74,6 +79,32 @@
</select>
<?php } ?>
</label>
<?php if ( $meta[ 'additional' ] ) {
$adicional = $meta[ 'additional' ];
$adicional_permissions = $adicional[ 'permissions' ];
$father = $permission;
?>
<b><?php echo $adicional[ 'label' ]; ?></b><br/>
<?php
foreach( $adicional_permissions as $permission => $meta ){
$description = $meta[ 'description' ];
$pattern = '/' . str_replace( 'ID' , '.*', $permission ) . '/';
$checked = ( $admin->hasPermission( $pattern, true ) ) ? 'checked="checked"' : '' ;
?>
<div class="row-fluid">
<label for="<?php echo $permission; ?>">
<span class="pull-right">
<input type="checkbox" <?php echo $checked; ?> class="icheck additional-permissions" father="<?php echo $father; ?>" value="1" name="<?php echo $permission; ?>" id="<?php echo $permission; ?>">
</span>
&nbsp;<?php echo $description; ?>
<span class="note">
&nbsp;(<?php echo $permission; ?>)
</span>
</label>
</div>
<?php
}
} ?>
</li>
<?php
}
@ -153,11 +184,48 @@
<script type="text/javascript">
$(function() {
$(document).on('click', '.admin-save', function() {
$(document).on( 'click', '.admin-save', function() {
sendForm();
} );
$( '#global' ).on( 'ifChanged', function(event){
validateChecked( '#global' );
});
$( '.do-all-father' ).on( 'ifChanged', function( event ){
validateDoAllPermissions( $( '#' + event.target.id ) );
});
$( '.do-all-father' ).each( function( index, el ){
validateDoAllPermissions( $( el ) );
} );
validateChecked( '#global' );
});
function validateDoAllPermissions( el ){
if( el.is( ':checked' ) ){
$( '.row-child-' + el.attr( 'family-name' ) ).hide();
} else {
$( '.row-child-' + el.attr( 'family-name' ) ).show();
}
}
function validateChecked( id ){
var el = $( id );
switch( id ){
case '#global':
if( el.is( ':checked' ) ){
$( '.permission-box' ).hide();
$( '.box-global' ).show();
} else {
$( '.permission-box' ).show();
}
break;
}
}
function sendForm(){
var data = {};
@ -165,20 +233,33 @@
$('.permissions').each( function(){
var el = $( this );
var type = null;
if( el.is( 'input' ) ){
if( el.is( 'input' ) && el.is( ':visible' ) ){
type = el.attr( 'type' );
if( type == 'checkbox' ){
if( el.is( ':checked' ) ){
data[ el.attr( 'name' ) ] = 1;
}
}
} else if( el.is( 'select' ) ){
} else if( el.is( 'select' ) && el.is( ':visible' ) ){
var options = el.val();
$( options ).each( function( index, val ){
data[ val ] = 1;
} );
}
} );
$('.additional-permissions').each( function(){
var el = $( this );
if( el.is( ':checked' ) && el.is( ':visible' ) ){
var father = el.attr( 'father' );
var pattern = new RegExp( father.replace( 'ID', '((.)*)' ) ,'gm');
var values = $( '#' + father ).val();
$( values ).each( function( index, val ){
var id = val.replace( pattern, "\$1");
var permission = el.attr( 'name' ).replace( 'ID', id );
data[ permission ] = 1;
} );
}
} );
var url = App.service + 'permissions/users/<?php echo $admin->id_admin; ?>/permissions';