Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 132 lines (115 sloc) 3.27 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
<?php

namespace UFCOE\AccessDefaults;

elgg_register_event_handler('init', 'system', __NAMESPACE__ . '\\init');

function init() {
spl_autoload_register(function ($class) {
if (0 === strpos($class, __NAMESPACE__ . '\\')) {
$file = __DIR__ . '/lib/' . strtr($class, '_\\', '//') . '.php';
is_file($file) && (require $file);
}
});

elgg_register_plugin_hook_handler(shortname() . ':set_default', 'after',
__NAMESPACE__ . '\\input_access_handler');

// add default case
elgg_register_plugin_hook_handler(shortname() . ':alter', 'before',
__NAMESPACE__ . '\\alter_access_handler');

elgg_register_plugin_hook_handler('access:collections:write', 'all',
__NAMESPACE__ . '\\alter_options_handler');
}

/**
* Get the shortname of this plugin
* @return string
*/
function shortname() {
return basename(__DIR__);
}

/**
* This determines the default access level for new content in our input/access view
*
* @param string $hook
* @param string $type
* @param int $returnvalue
* @param array $params
* @return int
*/
function input_access_handler($hook, $type, $returnvalue, $params) {
// context data to be considered for deciding the access level
$ctx = new LevelContext($returnvalue, $params);

// allow plugins to add their own AccessCases to be considered
$params['context'] = $ctx;
$modifiers = get_modifiers($params);

// check the list of cases, each may change the access level and choose to make their
// decision final
foreach ($modifiers as $modifier) {
/* @var ModifierInterface $modifier */
$returnvalue = $modifier->modifyAccessLevel($returnvalue, $ctx);
if ($modifier->isFinal()) {
break;
}
}

// store this for use in the access:collections:write hook
set_pop_ctx($ctx);

return $returnvalue;
}

/**
* Build/get the list of modifier objects use to alter levels/options
* @param array $params ignored after the first invocation
* @return array
*/
function get_modifiers(array $params) {
static $modifiers = null;
if ($modifiers === null) {
$modifiers = elgg_trigger_plugin_hook(shortname() . ':alter', 'before', $params, array());
}
return $modifiers;
}

/**
* Add default & issue4525 modifiers for this plugin
*
* @param string $hook
* @param string $type
* @param array $returnvalue
* @param array $params
* @return array
*/
function alter_access_handler($hook, $type, $returnvalue, $params) {
$returnvalue[] = new Modifier_Default();
$returnvalue[] = new Modifier_Issue4525();
return $returnvalue;
}

/**
* @param string $hook
* @param string $type
* @param array $returnvalue
* @param array $params
* @return array
*/
function alter_options_handler($hook, $type, $returnvalue, $params) {
$ctx = set_pop_ctx();
if ($ctx) {
$modifiers = get_modifiers($params);
foreach ($modifiers as $modifier) {
/* @var ModifierInterface $modifier */
$returnvalue = $modifier->modifyOptions($returnvalue, $ctx);
if ($modifier->isFinal()) {
break;
}
}
}
return $returnvalue;
}

/**
* Set or pop a level context
* @param null|LevelContext $ctx set to set, null to pop
* @return null|LevelContext
*/
function set_pop_ctx(LevelContext $ctx = null) {
static $cache = null;
if ($ctx) {
$cache = $ctx;
} else {
$ret = $cache;
$cache = null;
return $ret;
}
}
Something went wrong with that request. Please try again.