Merge pull request #8264 from owncloud/app-enable-by-group

Add the option to enable apps only for specific groups
This commit is contained in:
icewind1991 2014-06-03 19:01:12 +02:00
commit cdf8a123e7
6 changed files with 378 additions and 258 deletions

View File

@ -175,7 +175,8 @@
button.animate({'width':newWidth},undefined,undefined,function(){
button.css('width','');
});
list.animate({'width':newOuterWidth,'left':pos.left+3});
list.animate({'width':newOuterWidth,'left':pos.left});
self.change();
});
var li=$('<li></li>');
li.append(input).append(label);
@ -285,7 +286,7 @@
) {
list.css({
top:pos.top+button.outerHeight()-5,
left:pos.left+3,
left:pos.left,
width:(button.outerWidth()-2)+'px',
'max-height':($(document).height()-(button.offset().top+button.outerHeight()+10))+'px'
});
@ -296,7 +297,7 @@
list.css('max-height', $(document).height()-($(document).height()-(pos.top)+50)+'px');
list.css({
top:pos.top - list.height(),
left:pos.left+3,
left:pos.left,
width:(button.outerWidth()-2)+'px'
});

View File

@ -74,6 +74,7 @@ class OC_App{
/**
* load a single app
*
* @param string $app
*/
public static function loadApp($app) {
@ -85,8 +86,9 @@ class OC_App{
/**
* check if an app is of a specific type
*
* @param string $app
* @param string/array $types
* @param string|array $types
* @return bool
*/
public static function isType($app, $types) {
@ -104,6 +106,7 @@ class OC_App{
/**
* get the types of an app
*
* @param string $app
* @return array
*/
@ -137,6 +140,7 @@ class OC_App{
/**
* check if app is shipped
*
* @param string $appid the id of the app to check
* @return bool
*
@ -155,6 +159,7 @@ class OC_App{
* get all enabled apps
*/
private static $enabledAppsCache = array();
public static function getEnabledApps($forceRefresh = false) {
if (!OC_Config::getValue('installed', false)) {
return array();
@ -162,26 +167,29 @@ class OC_App{
if (!$forceRefresh && !empty(self::$enabledAppsCache)) {
return self::$enabledAppsCache;
}
$apps=array('files');
$sql = 'SELECT `appid` FROM `*PREFIX*appconfig`'
. ' WHERE `configkey` = \'enabled\' AND `configvalue`=\'yes\''
. ' ORDER BY `appid`';
if (OC_Config::getValue( 'dbtype', 'sqlite' ) === 'oci') {
//FIXME oracle hack: need to explicitly cast CLOB to CHAR for comparison
$sql = 'SELECT `appid` FROM `*PREFIX*appconfig`'
. ' WHERE `configkey` = \'enabled\' AND to_char(`configvalue`)=\'yes\''
. ' ORDER BY `appid`';
$appConfig = \OC::$server->getAppConfig();
$appStatus = $appConfig->getValues(false, 'enabled');
foreach ($appStatus as $app => $enabled) {
if ($app === 'files') {
continue;
}
$query = OC_DB::prepare( $sql );
$result=$query->execute();
if( \OC_DB::isError($result)) {
throw new DatabaseException($result->getMessage(), $query);
}
while($row=$result->fetchRow()) {
if(array_search($row['appid'], $apps)===false) {
$apps[]=$row['appid'];
if ($enabled === 'yes') {
$apps[] = $app;
} else if ($enabled !== 'no') {
$user = \OC_User::getUser();
$groups = json_decode($enabled);
if (is_array($groups)) {
foreach ($groups as $group) {
if (\OC_Group::inGroup($user, $group)) {
$apps[] = $app;
break;
}
}
}
}
}
sort($apps);
array_unshift($apps, 'files');
self::$enabledAppsCache = $apps;
return $apps;
}
@ -204,12 +212,13 @@ class OC_App{
/**
* enables an app
* @param mixed $app app
* @param array $groups (optional) when set, only these groups will have access to the app
* @throws \Exception
* @return void
*
* This function set an app as enabled in appconfig.
*/
public static function enable( $app ) {
public static function enable($app, $groups = null) {
self::$enabledAppsCache = array(); // flush
if (!OC_Installer::isInstalled($app)) {
// check if app is a shipped app or not. OCS apps have an integer as id, shipped apps use a string
@ -237,8 +246,12 @@ class OC_App{
array($info['name'])
)
);
} else {
if (!is_null($groups)) {
OC_Appconfig::setValue($app, 'enabled', json_encode($groups));
}else{
OC_Appconfig::setValue($app, 'enabled', 'yes');
}
if (isset($appdata['id'])) {
OC_Appconfig::setValue($app, 'ocsid', $appdata['id']);
}
@ -344,7 +357,8 @@ class OC_App{
$settings = array();
// by default, settings only contain the help menu
if (OC_Util::getEditionString() === '' &&
OC_Config::getValue('knowledgebaseenabled', true)==true) {
OC_Config::getValue('knowledgebaseenabled', true) == true
) {
$settings = array(
array(
"id" => "help",
@ -415,11 +429,11 @@ class OC_App{
foreach ($list as &$naventry) {
if ($naventry['id'] == $activeapp) {
$naventry['active'] = true;
}
else{
} else {
$naventry['active'] = false;
}
} unset( $naventry );
}
unset($naventry);
usort($list, create_function('$a, $b', 'if( $a["order"] == $b["order"] ) {return 0;}elseif( $a["order"] < $b["order"] ) {return -1;}else{return 1;}'));
@ -428,6 +442,7 @@ class OC_App{
/**
* Get the path where to install apps
*
* @return string
*/
public static function getInstallPath() {
@ -458,6 +473,7 @@ class OC_App{
}
return false;
}
/**
* Get the directory for the given app.
* If the app is defined in multiple directories, the first one is taken. (false if not found)
@ -583,6 +599,7 @@ class OC_App{
/**
* get the id of loaded app
*
* @return string
*/
public static function getCurrentApp() {
@ -636,6 +653,7 @@ class OC_App{
/**
* register an admin form to be shown
*
* @param string $app
* @param string $page
*/
@ -714,10 +732,15 @@ class OC_App{
continue;
}
if ( OC_Appconfig::getValue( $app, 'enabled', 'no') == 'yes' ) {
$enabled = OC_Appconfig::getValue($app, 'enabled', 'no');
$info['groups'] = null;
if ($enabled === 'yes') {
$active = true;
} else {
} else if($enabled === 'no') {
$active = false;
} else {
$active = true;
$info['groups'] = $enabled;
}
$info['active'] = $active;
@ -854,6 +877,7 @@ class OC_App{
/**
* check if the app needs updating and update when needed
*
* @param string $app
*/
public static function checkUpgrade($app) {
@ -873,8 +897,7 @@ class OC_App{
try {
OC_App::updateApp($app);
OC_Hook::emit('update', 'success', 'Updated ' . $info['name'] . ' app');
}
catch (Exception $e) {
} catch (Exception $e) {
OC_Hook::emit('update', 'failure', 'Failed to update ' . $info['name'] . ' app: ' . $e->getMessage());
$l = OC_L10N::get('lib');
throw new RuntimeException($l->t('Failed to upgrade "%s".', array($app)), 0, $e);
@ -1009,6 +1032,7 @@ class OC_App{
/**
* update the database for the app and call the update script
*
* @param string $appid
*/
public static function updateApp($appid) {

View File

@ -3,8 +3,10 @@
OC_JSON::checkAdminUser();
OCP\JSON::callCheck();
$groups = isset($_POST['groups']) ? $_POST['groups'] : null;
try {
OC_App::enable(OC_App::cleanAppId($_POST['appid']));
OC_App::enable(OC_App::cleanAppId($_POST['appid']), $groups);
OC_JSON::success();
} catch (Exception $e) {
OC_Log::write('core', $e->getMessage(), OC_Log::ERROR);

View File

@ -25,13 +25,16 @@ OC_Util::checkAdminUser();
// Load the files we need
OC_Util::addStyle( "settings", "settings" );
OC_Util::addScript("core", "multiselect");
OC_App::setActiveNavigationEntry( "core_apps" );
$combinedApps = OC_App::listAllApps();
$groups = \OC_Group::getGroups();
$tmpl = new OC_Template( "settings", "apps", "user" );
$tmpl->assign('apps', $combinedApps);
$tmpl->assign('groups', $groups);
$appid = (isset($_GET['appid'])?strip_tags($_GET['appid']):'');

View File

@ -88,12 +88,41 @@ OC.Settings.Apps = OC.Settings.Apps || {
} else {
page.find(".warning").hide();
}
page.find("div.multiselect").parent().remove();
if(OC.Settings.Apps.isType(app, 'filesystem') || OC.Settings.Apps.isType(app, 'prelogin') ||
OC.Settings.Apps.isType(app, 'authentication') || OC.Settings.Apps.isType(app, 'logging')) {
page.find("#groups_enable").hide();
page.find("label[for='groups_enable']").hide();
page.find("#groups_enable").attr('checked', null);
} else {
$('#group_select > option').each(function (i, el) {
if (app.groups.length === 0 || app.groups.indexOf(el.value) >= 0) {
$(el).attr('selected', 'selected');
} else {
$(el).attr('selected', null);
}
});
if (app.active) {
if (app.groups.length) {
$('#group_select').multiSelect();
page.find("#groups_enable").attr('checked','checked');
} else {
page.find("#groups_enable").attr('checked', null);
}
page.find("#groups_enable").show();
page.find("label[for='groups_enable']").show();
} else {
page.find("#groups_enable").hide();
page.find("label[for='groups_enable']").hide();
}
}
},
enableApp:function(appid, active, element) {
console.log('enableApp:', appid, active, element);
enableApp:function(appid, active, element, groups) {
groups = groups || [];
var appitem=$('#app-navigation ul li[data-id="'+appid+'"]');
element.val(t('settings','Please wait....'));
if(active) {
if(active && !groups.length) {
$.post(OC.filePath('settings','ajax','disableapp.php'),{appid:appid},function(result) {
if(!result || result.status !== 'success') {
if (result.data && result.data.message) {
@ -108,14 +137,19 @@ OC.Settings.Apps = OC.Settings.Apps || {
}
else {
appitem.data('active',false);
appitem.data('groups', '');
element.data('active',false);
OC.Settings.Apps.removeNavigation(appid);
appitem.removeClass('active');
element.val(t('settings','Enable'));
element.parent().find("#groups_enable").hide();
element.parent().find("label[for='groups_enable']").hide();
var app = OC.get('appData_' + appid);
app.active = false;
}
},'json');
} else {
$.post(OC.filePath('settings','ajax','enableapp.php'),{appid:appid},function(result) {
$.post(OC.filePath('settings','ajax','enableapp.php'),{appid: appid, groups: groups},function(result) {
if(!result || result.status !== 'success') {
if (result.data && result.data.message) {
OC.Settings.Apps.showErrorMessage(result.data.message);
@ -132,6 +166,21 @@ OC.Settings.Apps = OC.Settings.Apps || {
element.data('active',true);
appitem.addClass('active');
element.val(t('settings','Disable'));
var app = OC.get('appData_' + appid);
app.active = true;
if (OC.Settings.Apps.isType(app, 'filesystem') || OC.Settings.Apps.isType(app, 'prelogin') ||
OC.Settings.Apps.isType(app, 'authentication') || OC.Settings.Apps.isType(app, 'logging')) {
element.parent().find("#groups_enable").hide();
element.parent().find("label[for='groups_enable']").hide();
} else {
element.parent().find("#groups_enable").show();
element.parent().find("label[for='groups_enable']").show();
if (groups) {
appitem.data('groups', JSON.stringify(groups));
} else {
appitem.data('groups', '');
}
}
}
},'json')
.fail(function() {
@ -145,7 +194,6 @@ OC.Settings.Apps = OC.Settings.Apps || {
}
},
updateApp:function(appid, element) {
console.log('updateApp:', appid, element);
element.val(t('settings','Updating....'));
$.post(OC.filePath('settings','ajax','updateapp.php'),{appid:appid},function(result) {
if(!result || result.status !== 'success') {
@ -238,12 +286,18 @@ OC.Settings.Apps = OC.Settings.Apps || {
showErrorMessage: function(message) {
$('.appinfo .warning').show();
$('.appinfo .warning').text(message);
},
isType: function(app, type){
return app.types && app.types.indexOf(type) !== -1;
}
};
$(document).ready(function(){
$('#app-navigation ul li').each(function(index,li){
var app = OC.get('appData_'+$(li).data('id'));
if (app) {
app.groups= $(li).data('groups') || [];
}
$(li).data('app',app);
$(this).find('span.hidden').remove();
});
@ -281,6 +335,20 @@ $(document).ready(function(){
}
});
$('#group_select').change(function() {
var element = $('#app-content input.enable');
var groups = $(this).val();
var appid = element.data('appid');
if (appid) {
OC.Settings.Apps.enableApp(appid, false, element, groups);
var li = $('[data-id="'+appid+'"]');
var app = OC.get('appData_' + $(li).data('id'));
app.groups = groups;
li.data('groups', groups);
li.attr('data-groups', JSON.stringify(groups));
}
});
if(appid) {
var item = $('#app-navigation ul li[data-id="'+appid+'"]');
if(item) {
@ -289,4 +357,16 @@ $(document).ready(function(){
$('#app-navigation').animate({scrollTop: $(item).offset().top-70}, 'slow','swing');
}
}
$("#groups_enable").change(function() {
if (this.checked) {
$("div.multiselect").parent().remove();
$('#group_select').multiSelect();
} else {
$('#group_select').hide().val(null);
$("div.multiselect").parent().remove();
}
$('#group_select').change();
});
});

View File

@ -16,7 +16,7 @@
<?php endif; ?>
<?php foreach($_['apps'] as $app):?>
<li <?php if($app['active']) print_unescaped('class="active"')?> data-id="<?php p($app['id']) ?>"
<li <?php if($app['active']) print_unescaped('class="active"')?> data-id="<?php p($app['id']) ?>" data-groups="<?php p($app['groups']) ?>"
<?php if ( isset( $app['ocs_id'] ) ) { print_unescaped("data-id-ocs=\"{".OC_Util::sanitizeHTML($app['ocs_id'])."}\""); } ?>
data-type="<?php p($app['internal'] ? 'internal' : 'external') ?>" data-installed="1">
<a class="app<?php if(!$app['internal']) p(' externalapp') ?>"
@ -53,6 +53,16 @@
print_unescaped($l->t('<span class="licence"></span>-licensed by <span class="author"></span>'));?></p>
<input class="enable hidden" type="submit" />
<input class="update hidden" type="submit" value="<?php p($l->t('Update')); ?>" />
<br />
<input class="hidden" type="checkbox" id="groups_enable"/>
<label class="hidden" for="groups_enable"><?php p($l->t('Enable only for specific groups')); ?></label>
<br />
<select class="hidden" id="group_select" multiple="multiple" title="<?php p($l->t('All')); ?>">
<?php foreach($_['groups'] as $group):?>
<option value="<?php p($group);?>"><?php p($group); ?></option>
<?php endforeach;?>
</select>
<div class="warning hidden"></div>
</div>
</div>