summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/page.inc.php
blob: 041ae74f8b8f4f06948bd84233045ddef754804c (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
<?php

class Page_RebootControl extends Page
{

	private $action = false;

	/**
	 * 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
		}

		$this->action = Request::any('action', 'show', 'string');


		if ($this->action === 'reboot' || $this->action === 'shutdown') {

			$requestedClients = Request::post('clients', false, 'array');
			if (!is_array($requestedClients) || empty($requestedClients)) {
				Message::addError('no-clients-selected');
				Util::redirect();
			}
			$minutes = Request::post('minutes', 0, 'int');

			$actualClients = RebootQueries::getMachinesByUuid($requestedClients);
			if (count($actualClients) !== count($requestedClients)) {
				// We could go ahead an see which ones were not found in DB but this should not happen anyways unless the
				// user manipulated the request
				Message::addWarning('some-machine-not-found');
			}
			// Filter ones with no permission
			foreach (array_keys($actualClients) as $idx) {
				if (!User::hasPermission('action.' . $this->action, $actualClients[$idx]['locationid'])) {
					Message::addWarning('locations.no-permission-location', $actualClients[$idx]['locationid']);
					unset($actualClients[$idx]);
				} else {
					$locationId = $actualClients[$idx]['locationid'];
				}
			}
			// See if anything is left
			if (!is_array($actualClients) || empty($actualClients)) {
				Message::addError('no-clients-selected');
				Util::redirect();
			}

			$task = RebootControl::execute($actualClients, $this->action === 'shutdown', $minutes, $locationId);
			Util::redirect("?do=rebootcontrol&taskid=".$task["id"]);
		}

	}

	/**
	 * Menu etc. has already been generated, now it's time to generate page content.
	 */

	protected function doRender()
	{
		if ($this->action === 'show') {

			$data = [];
			$taskId = Request::get("taskid");

			if ($taskId && Taskmanager::isTask($taskId)) {
				$task = Taskmanager::status($taskId);
				$data['taskId'] = $taskId;
				$data['locationId'] = $task['data']['locationId'];
				$data['locationName'] = Location::getName($task['data']['locationId']);
				$data['clients'] = $task['data']['clients'];
				Render::addTemplate('status', $data);
			} else {

				//location you want to see, default are "not  assigned" clients
				$requestedLocation = Request::get('location', false, 'int');
				$allowedLocs = User::getAllowedLocations("action.*");
				if (empty($allowedLocs)) {
					User::assertPermission('action.*');
				}

				if ($requestedLocation === false) {
					if (in_array(0, $allowedLocs)) {
						$requestedLocation = 0;
					} else {
						$requestedLocation = reset($allowedLocs);
					}
				}

				$data['locations'] = Location::getLocations($requestedLocation, 0, true);

				// disable each location user has no permission for
				foreach ($data['locations'] as &$loc) {
					if (!in_array($loc["locationid"], $allowedLocs)) {
						$loc["disabled"] = "disabled";
					}
				}
				// Always show public key (it's public, isn't it?)
				$data['pubKey'] = SSHKey::getPublicKey();

				// Only enable shutdown/reboot-button if user has permission for the location
				Permission::addGlobalTags($data['perms'], $requestedLocation, ['newkeypair', 'action.shutdown', 'action.reboot']);

				Render::addTemplate('header', $data);

				// only fill table if user has at least one permission for the location
				if (!in_array($requestedLocation, $allowedLocs)) {
					Message::addError('locations.no-permission-location', $requestedLocation);
				} else {
					$data['data'] = RebootQueries::getMachineTable($requestedLocation);
					Render::addTemplate('_page', $data);
				}

			}
		}
	}

	function doAjax()
	{
		$this->action = Request::post('action', false, 'string');
		if ($this->action === 'generateNewKeypair') {
			User::assertPermission("newkeypair");
			Property::set("rebootcontrol-private-key", false);
			echo SSHKey::getPublicKey();
		} else {
			echo 'Invalid action.';
		}
	}



}