summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/page.inc.php
blob: db7882d95532fd30f26fcac7c27cb6dd549e472b (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
<?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 === 'startReboot' || $this->action === 'startShutdown') {
			$clients = Request::post('clients');
			if (!is_array($clients) || empty($clients)) {
				Message::addError('no-clients-selected');
				Util::redirect();
			}
			$locationId = Request::post('locationId', false, 'int');
			if ($locationId === false) {
				Message::addError('locations.invalid-location-id', $locationId);
				Util::redirect();
			}
			$shutdown = $this->action === "startShutdown";
			$minutes = Request::post('minutes', 0, 'int');
			$privKey = SSHKey::getPrivateKey();

			$list = RebootQueries::getMachinesByUuid($clients);
			if (count($list) !== count($clients)) {
				// 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');
			}
			// TODO: Iterate over list and check if a locationid is not in permissions
			// TODO: we could also check if the locationid is equal or a sublocation of the $locationId from above
			// (this would be more of a sanity check though, or does the UI allow selecting machines from different locations)

			$task = RebootControl::execute($list, $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') {

			$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', 0, 'int');

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

				$data['pubKey'] = SSHKey::getPublicKey();

				Render::addTemplate('_page', $data);
			}
		}
	}

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



}