summaryrefslogtreecommitdiffstats
path: root/modules-available/permissionmanager/page.inc.php
blob: 700ed96a545de6673cf753bcd183c2a3b9b15884 (plain) (blame)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php

class Page_PermissionManager extends Page
{

	/**
	 * Called before any page rendering happens - early hook to check parameters etc.
	 */
	protected function doPreprocess()
	{
		User::load();

		if (!User::isLoggedIn()) {
			Message::addError('main.no-permission');
			Util::redirect('?do=Main'); // does not return
		}

		$action = Request::any('action', 'show', 'string');
		if ($action === 'addRoleToUser') {
			$users = Request::post('users', '');
			$roles = Request::post('roles', '');
			DbUpdate::addRoleToUser($users, $roles);
		} elseif ($action === 'removeRoleFromUser') {
			$users = Request::post('users', '');
			$roles = Request::post('roles', '');
			DbUpdate::removeRoleFromUser($users, $roles);
		} elseif ($action === 'deleteRole') {
			$id = Request::post('deleteId', false, 'string');
			DbUpdate::deleteRole($id);
		} elseif ($action === 'saveRole') {
			$roleID = Request::post("roleid", false);
			$roleName = Request::post("roleName");
			$locations = Request::post("allLocations", "off") == "on" ? array(0) : Request::post("locations");
			$permissions = Request::post("permissions");
			DbUpdate::saveRole($roleName, $locations, $permissions, $roleID);
		}
	}

	/**
	 * Menu etc. has already been generated, now it's time to generate page content.
	 */
	protected function doRender()
	{
		$show = Request::get("show", "roles");

		// switch between tables, but always show menu to switch tables
		if ( $show === 'roles' || $show === 'users' || $show === 'locations' ) {
			// get menu button colors
			$buttonColors = self::setButtonColors($show);

			$data = array();

			Render::openTag('div', array('class' => 'row'));
			Render::addtemplate('_page', $buttonColors);
			Render::closeTag('div');

			if ($show === "roles") {
				$data = array("roles" => GetData::getRoles());
				Render::addTemplate('rolesTable', $data);
			} elseif ($show === "users") {
				$data = array("user" => GetData::getUserData(), "roles" => GetData::getRoles());
				Render::addTemplate('usersTable', $data);
			} elseif ($show === "locations") {
				$data = array("location" => GetData::getLocationData());
				Render::addTemplate('locationsTable', $data);
			}
		} elseif ($show === "roleEditor") {
			$data = array();

			$roleID = Request::get("roleid", false);
			$selectedLocations = array();
			if ($roleID) {
				$roleData = GetData::getRoleData($roleID);
				$data["roleid"] = $roleID;
				$data["roleName"] = $roleData["name"];
				if (count($roleData["locations"]) == 1 && $roleData["locations"][0] == 0) {
					$data["allLocChecked"] = "checked";
					$data["selectizeClass"] = "disabled";
				} else {
					$data["allLocChecked"] = "";
					$data["selectizeClass"] = "";
					$selectedLocations = $roleData["locations"];
				}
				$data["selectedPermissions"] = implode(" ", $roleData["permissions"]);
			} else {
				$data["allLocChecked"] = "checked";
				$data["selectizeClass"] = "disabled";
			}

			$permissions = PermissionUtil::getPermissions();
			$permissionHTML = "";
			foreach ($permissions as $k => $v) {
				$name = Dictionary::translateFileModule($k, "module", "module_name");
				$permissionHTML .= "
				<div id='$k' class='panel panel-primary module-box' style='display: none;'>
					<div class='panel-heading'>
						<div class='checkbox'>
							<input name='permissions[]' value='$k.*' type='checkbox' class='form-control'>
							<label>$name</label>
						</div>
					</div>
					<div class='panel-body'>
			";
				$permissionHTML .= self::generateSubPermissionHTML($v, $k);
				$permissionHTML .= "</div></div>";
			}

			$data["locations"] = GetData::getLocations($selectedLocations);
			$data["moduleNames"] = array();
			foreach (array_keys($permissions) as $moduleid) {
				$data["moduleNames"][] = array("id" => $moduleid,
					"name" => Dictionary::translateFileModule($moduleid, "module", "module_name"));
			}

			$data["permissionHTML"] = $permissionHTML;
			Render::addTemplate('roleEditor', $data);
		}
	}

	// Menu: Selected table is shown in blue (btn-primary)
	private function setButtonColors($show) {
		if ($show === 'roles') {
			$buttonColors['rolesButtonClass'] = 'btn-primary';
			$buttonColors['usersButtonClass'] = 'btn-default';
			$buttonColors['locationsButtonClass'] = 'btn-default';
		} elseif ($show === 'users') {
			$buttonColors['rolesButtonClass'] = 'btn-default';
			$buttonColors['usersButtonClass'] = 'btn-primary';
			$buttonColors['locationsButtonClass'] = 'btn-default';
		} elseif ($show === 'locations') {
			$buttonColors['rolesButtonClass'] = 'btn-default';
			$buttonColors['usersButtonClass'] = 'btn-default';
			$buttonColors['locationsButtonClass'] = 'btn-primary';
		}  else {
			$buttonColors['rolesButtonClass'] = 'btn-default';
			$buttonColors['usersButtonClass'] = 'btn-default';
			$buttonColors['locationsButtonClass'] = 'btn-default';
		}

		return $buttonColors;
	}

	private static function generateSubPermissionHTML($subPermissions, $permissionString)
	{
		$html = "<ul class='list-group'>";
		foreach ($subPermissions as $k => $v) {
			$tmpPermString = $permissionString.".".$k;
			$checkBoxValue  = $tmpPermString;
			if (is_array($v)) {
				$checkBoxValue .= ".*";
			} else {
				$k .= " - ".$v;
			}
			$html .= "
				<li class='list-group-item'>
					<div class='checkbox'>
						<input name='permissions[]' value='$checkBoxValue' type='checkbox' class='form-control'>
						<label>$k</label>
					</div>
			";
			if (is_array($v)) {
				$html .= self::generateSubPermissionHTML($v, $tmpPermString);
			}
			$html .= "</li>";
		}
		$html .= "</ul>";
		return $html;
	}

}