admin_cmd_acl::get_change_widgets()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * EGroupware admin - admin command: give or remove run rights from a given account and application
4
 *
5
 * @link http://www.egroupware.org
6
 * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
7
 * @package admin
8
 * @copyright (c) 2007-16 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
9
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
10
 * @version $Id$
11
 */
12
13
use EGroupware\Api;
14
15
/**
16
 * admin command: give or remove run rights from a given account and application
17
 */
18
class admin_cmd_acl extends admin_cmd
19
{
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param boolean|array $allow true=give rights, false=remove rights, or array with all params
24
	 * @param string|int $account =null account name or id
25
	 * @param array|string $app =null app-name
26
	 * @param string $location =null ACL location.  Usually a user or group ID, but may also be any app-specific string
27
	 * @param int $rights =null ACL rights.  See Api\ACL.
28
	 */
29
	function __construct($allow,$account=null,$app=null,$location=null,$rights=null)
30
	{
31
		if (!is_array($allow))
32
		{
33
			$allow = array(
34
				'allow' => $allow,
35
				'account' => $account,
36
				'app' => $app,
37
				'location' => $location,
38
				'rights' => (int)$rights
39
			);
40
		}
41
42
		// Make sure we only deal with real add/remove changes
43
44
		admin_cmd::__construct($allow);
45
	}
46
47
	/**
48
	 * give or remove run rights from a given account and application
49
	 *
50
	 * @param boolean $check_only =false only run the checks (and throw the exceptions), but not the command itself
51
	 * @return string success message
52
	 * @throws Api\Exception\NoPermission\Admin
53
	 * @throws Api\Exception\WrongUserinput(lang("Unknown account: %1 !!!",$this->account),15);
54
	 * @throws Api\Exception\WrongUserinput(lang("Application '%1' not found (maybe not installed or misspelled)!",$name),8);
55
	 */
56
	protected function exec($check_only=false)
57
	{
58
		$account_id = admin_cmd::parse_account($this->account);
59
60
61
		list($app) = admin_cmd::parse_apps(array($this->app));
62
		$location = $this->location;
0 ignored issues
show
Bug Best Practice introduced by
The property location does not exist on admin_cmd_acl. Since you implemented __get, consider adding a @property annotation.
Loading history...
63
		$rights = (int)$this->rights;
0 ignored issues
show
Bug Best Practice introduced by
The property rights does not exist on admin_cmd_acl. Since you implemented __get, consider adding a @property annotation.
Loading history...
64
65
66
		$old_rights = (int)$GLOBALS['egw']->acl->get_specific_rights_for_account($account_id, $location, $app);
67
		$new_rights = max(0,$old_rights + (($this->allow ? 1 : -1) * $rights));
0 ignored issues
show
Bug Best Practice introduced by
The property allow does not exist on admin_cmd_acl. Since you implemented __get, consider adding a @property annotation.
Loading history...
68
69
		$this->set = $new_rights;
70
		$this->old = $old_rights;
0 ignored issues
show
Documentation Bug introduced by
It seems like $old_rights of type integer is incompatible with the declared type array of property $old.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
		if ($check_only) return true;
72
73
		//echo "account=$this->account, account_id=$account_id, apps: ".implode(', ',$apps)."\n";
74
		admin_cmd::_instanciate_acl($account_id);
75
76
		if ($new_rights)
77
		{
78
			admin_cmd::$acl->add_repository($app,$location,$account_id,$new_rights);
79
		}
80
		else
81
		{
82
			admin_cmd::$acl->delete_repository($app,$location,$account_id);
83
		}
84
		return lang('Applications run rights updated.');
85
	}
86
87
	/**
88
	 * Return a title / string representation for a given command, eg. to display it
89
	 *
90
	 * @return string
91
	 */
92
	function __tostring()
93
	{
94
		$rights = $this->rights;
0 ignored issues
show
Bug Best Practice introduced by
The property rights does not exist on admin_cmd_acl. Since you implemented __get, consider adding a @property annotation.
Loading history...
95
		$location = lang($this->location);
0 ignored issues
show
Bug Best Practice introduced by
The property location does not exist on admin_cmd_acl. Since you implemented __get, consider adding a @property annotation.
Loading history...
96
97
		if($this->location == 'run')
98
		{
99
			$rights = lang('run');
100
		}
101
		$names = Api\Hooks::single(array(
102
			'location' => 'acl_rights'
103
		), $this->app);
104
		if($names[$rights])
105
		{
106
			$rights = lang($names[$rights]);
107
		}
108
109
		if(is_numeric($this->location))
110
		{
111
			$location = admin_cmd::display_account($this->location);
112
		}
113
		return lang('%1 %2 rights for %3 on %4 to %5',
114
			$this->allow ? lang('Grant') : lang('Remove'),
0 ignored issues
show
Bug Best Practice introduced by
The property allow does not exist on admin_cmd_acl. Since you implemented __get, consider adding a @property annotation.
Loading history...
Unused Code introduced by
The call to lang() has too many arguments starting with $this->allow ? lang('Grant') : lang('Remove'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
		return /** @scrutinizer ignore-call */ lang('%1 %2 rights for %3 on %4 to %5',

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
115
			$rights,
116
			admin_cmd::display_account($this->account),
117
			$this->app,
118
			$location
119
		);
120
	}
121
122
	/**
123
	 * Return (human readable) labels for keys of changes
124
	 *
125
	 * @return array
126
	 */
127
	function get_change_labels()
128
	{
129
		$labels = parent::get_change_labels();
130
		$labels[get_class($this)] = lang('ACL');
131
		return $labels;
132
	}
133
134
135
	/**
136
	 * Return widget types (indexed by field key) for changes
137
	 *
138
	 * Used by historylog widget to show the changes the command recorded.
139
	 */
140
	function get_change_widgets()
141
	{
142
		$widgets = parent::get_change_widgets();
143
		// Specify app to get bitwise permissions, since it's not always admin
144
		$widgets[get_class($this)] = 'select-bitwise';
145
146
		// Get select options for this app, slide them in via modifications
147
		// since historylog doesn't do attributes on value widgets
148
		Api\Etemplate::setElementAttribute('history['.get_class($this).']', 'select_options',
149
				Api\Etemplate\Widget\Select::typeOptions('select-bitwise', ','.$this->app)
150
		);
151
		return $widgets;
152
	}
153
}
154