new: Add default role to the user creation, fixes #256

pull/1486/head
iglocska 2016-08-29 14:49:14 +02:00
parent 234fc16894
commit df6baa01f4
9 changed files with 99 additions and 14 deletions

View File

@ -62,6 +62,8 @@ class RolesController extends AppController {
public function admin_index() {
if (!$this->_isSiteAdmin()) $this->redirect(array('controller' => 'roles', 'action' => 'index', 'admin' => false));
$this->AdminCrud->adminIndex();
$this->loadModel('AdminSetting');
$this->set('default_role_id', $this->AdminSetting->getSetting('default_role'));
$this->set('permFlags', $this->Role->permFlags);
$this->set('options', $this->options);
}
@ -83,6 +85,21 @@ class RolesController extends AppController {
$this->recursive = 0;
$this->set('permFlags', $this->Role->permFlags);
$this->set('list', $this->paginate());
$this->loadModel('AdminSetting');
$this->set('default_role_id', $this->AdminSetting->getSetting('default_role'));
$this->set('options', $this->options);
}
public function admin_set_default($role_id = false) {
if (!is_numeric($role_id) && $role_id !== false) {
return new CakeResponse(array('body'=> json_encode(array('saved' => false, 'errors' => 'Invalid role.')),'status'=>200));
}
$this->loadModel('AdminSetting');
$result = $this->AdminSetting->changeSetting('default_role', $role_id);
if ($result === true) {
return new CakeResponse(array('body'=> json_encode(array('saved' => true, 'success' => $role_id ? 'Default role set.' : 'Default role unset.')),'status'=>200));
} else {
return new CakeResponse(array('body'=> json_encode(array('saved' => false, 'errors' => $result)),'status'=>200));
}
}
}

View File

@ -365,6 +365,8 @@ class UsersController extends AppController {
if (!empty($t['Server']['name'])) $servers[$t['Server']['id']] = $t['Server']['name'];
else $servers[$t['Server']['id']] = $t['Server']['url'];
}
$this->loadModel('AdminSetting');
$this->set('default_role_id', $this->AdminSetting->getSetting('default_role'));
$this->set('servers', $servers);
$this->set(compact('roles'));
$this->set(compact('syncRoles'));

View File

@ -0,0 +1,46 @@
<?php
App::uses('AppModel', 'Model');
class AdminSetting extends AppModel {
public $useTable = 'admin_settings';
public $actsAs = array(
'SysLogLogable.SysLogLogable' => array(
'userModel' => 'User',
'userKey' => 'user_id',
'change' => 'full'
),
'Containable'
);
public $validate = array('setting' => 'isUnique');
public function changeSetting($setting, $value = false) {
$setting_object = $this->find('first', array(
'conditions' => array('setting' => $setting)
));
if (!empty($setting_object)) {
$setting_object['AdminSetting']['value'] = $value;
} else {
$this->create();
$setting_object['AdminSetting'] = array('setting' => $setting, 'value' => $value);
}
if ($this->save($setting_object)) {
return true;
} else {
return $this->validationErrors;
}
}
public function getSetting($setting) {
$setting_object = $this->find('first', array(
'conditions' => array('setting' => $setting)
));
if (!empty($setting_object)) {
return $setting_object['AdminSetting']['value'];
} else {
return false;
}
}
}

View File

@ -1,9 +0,0 @@
<?php
App::uses('AppModel', 'Model');
class AdminSetting extends AppModel {
public $actsAs = array('Containable');
public $validate = array('setting' => 'isUnique');
}

View File

@ -429,6 +429,7 @@ class AppModel extends Model {
break;
case '2.4.51':
$sqlArray[] = 'ALTER TABLE `servers` ADD `internal` tinyint(1) NOT NULL DEFAULT 0;';
$sqlArray[] = 'ALTER TABLE `roles` ADD `default_role` tinyint(1) NOT NULL DEFAULT 0;';
break;
case 'fixNonEmptySharingGroupID':
$sqlArray[] = 'UPDATE `events` SET `sharing_group_id` = 0 WHERE `distribution` != 4;';

View File

@ -19,6 +19,7 @@
<table class="table table-striped table-hover table-condensed">
<tr>
<th><?php echo $this->Paginator->sort('id');?></th>
<th>Default</th>
<th><?php echo $this->Paginator->sort('name');?></th>
<th><?php echo $this->Paginator->sort('permission', 'Permission');?></th>
<?php
@ -32,9 +33,10 @@
</tr><?php
foreach ($list as $item): ?>
<tr>
<td class="short"><?php echo $this->Html->link(h($item['Role']['id']), array('admin' => true, 'action' => 'edit', $item['Role']['id'])); ?>&nbsp;</td>
<td><?php echo $this->Html->link(h($item['Role']['id']), array('admin' => true, 'action' => 'edit', $item['Role']['id'])); ?>&nbsp;</td>
<td class="short" style="text-align:center;width:20px;"><input class="servers_default_role_checkbox" type="checkbox" data-id="<?php echo h($item['Role']['id']); ?>" <?php if ($default_role_id && $default_role_id == $item['Role']['id']) echo 'checked'; ?>></td>
<td><?php echo h($item['Role']['name']); ?>&nbsp;</td>
<td class="short"><?php echo h($options[$item['Role']['permission']]); ?>&nbsp;</td>
<td><?php echo h($options[$item['Role']['permission']]); ?>&nbsp;</td>
<?php foreach ($permFlags as $k => $flags): ?>
<td class="short"><span class="<?php if ($item['Role'][$k]) echo 'icon-ok'; ?>"></span>&nbsp;</td>
<?php endforeach; ?>
@ -61,7 +63,6 @@ endforeach; ?>
?>
</ul>
</div>
</div>
<?php
echo $this->element('side_menu', array('menuList' => 'admin', 'menuItem' => 'indexRole'));

View File

@ -19,6 +19,7 @@
<table class="table table-striped table-hover table-condensed">
<tr>
<th><?php echo $this->Paginator->sort('id');?></th>
<th>Default</th>
<th><?php echo $this->Paginator->sort('name');?></th>
<th><?php echo $this->Paginator->sort('permission', 'Permission');?></th>
<?php
@ -32,6 +33,7 @@
foreach ($list as $item): ?>
<tr>
<td class="short"><?php echo h($item['Role']['id']); ?>&nbsp;</td>
<td class="short" style="text-align:center;width:20px;"><div class="icon-<?php echo $default_role_id == $item['Role']['id'] ? 'ok' : 'remove' ?>"></div></td>
<td><?php echo h($item['Role']['name']); ?>&nbsp;</td>
<td class="short"><?php echo h($options[$item['Role']['permission']]); ?>&nbsp;</td>
<?php foreach ($permFlags as $k => $flags): ?>

View File

@ -48,7 +48,12 @@
'empty' => 'Choose organisation',
));
}
echo $this->Form->input('role_id', array('label' => 'Role'));
$roleOptions = array('label' => 'Role');
// We need to make sure that the default role is actually available to the admin (for an org admin it might not be)
if (!empty($default_role_id) && isset($roles[intval($default_role_id)])) {
$roleOptions['default'] = $default_role_id;
}
echo $this->Form->input('role_id', $roleOptions);
echo $this->Form->input('authkey', array('value' => $authkey, 'readonly' => 'readonly', 'div' => 'input clear'));
echo $this->Form->input('nids_sid');
?>

View File

@ -2496,4 +2496,24 @@ function serverOwnerOrganisationChange(host_org_id) {
$('#ServerInternal').prop("checked", false);
$('#InternalDiv').hide();
}
}
}
$('.servers_default_role_checkbox').click(function() {
var id = $(this).data("id");
var state = $(this).is(":checked");
$(".servers_default_role_checkbox").not(this).attr('checked', false);
$.ajax({
beforeSend: function (XMLHttpRequest) {
$(".loading").show();
},
success:function (data, textStatus) {
handleGenericAjaxResponse(data);
},
complete:function() {
$(".loading").hide();
},
type:"get",
cache: false,
url: '/admin/roles/set_default/' + (state ? id : ""),
});
});