aboutsummaryrefslogtreecommitdiff
path: root/admin/Application/Controllers/AdminusersController.php
blob: 0154c0cb6323615b21cabc18b22b105c50c523ec (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php

// h-admin, a web software to build a community of people that want to share their hardware information.
// Copyright (C) 2010  Antonio Gallo (h-admin-copyright.txt)
//
// This file is part of h-admin
//
// h-admin is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// h-admin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with h-admin.  If not, see <http://www.gnu.org/licenses/>.

if (!defined('EG')) die('Direct access not allowed!');

class AdminusersController extends Controller
{

	function __construct($model, $controller, $queryString)
	{
		parent::__construct($model, $controller, $queryString);

		$this->load('header_back');
		$this->load('footer_back','last');

		$this->helper('Menu','adminusers','panel/main');
		$this->helper('Array');

		$this->session('admin');
		$this->model();

		$this->m['AdminusersModel']->setFields('username:sanitizeAll,password:sha1','none');

		$this->m['AdminusersModel']->strongConditions['update'] = array('checkAlphaNum'=>'username','checkEqual'=>'password,confirmation');
		$this->m['AdminusersModel']->strongConditions['insert'] = array('checkAlphaNum'=>'username','checkEqual'=>'password,confirmation');

		$this->m['AdminusersModel']->databaseConditions['insert'] = array('checkUnique'=>'username');
		$this->m['AdminusersModel']->databaseConditions['update'] = array('checkUniqueCompl'=>'username');

		$this->setArgKeys(array('page:forceNat'=>1,'id_group:forceInt'=>'0','token:sanitizeAll'=>'token'));
		Params::$nullQueryValue = '0';
	}

	public function login()
	{
		$data['action'] = Url::getRoot('adminusers/login');
		$data['notice'] = null;
		
		$this->s['admin']->checkStatus();
		if ($this->s['admin']->status['status']=='logged')
		{
			$this->s['admin']->redirect('logged');
		}
		if (isset($_POST['username']) and isset($_POST['password']))
		{
			$username = ctype_alnum($_POST['username']) ? sanitizeAll($_POST['username']) : '';
			$choice = $this->s['admin']->login($username,$_POST['password']);

			switch($choice)
			{
				case 'logged':
					$this->redirect('panel/main',3,'You are already logged...');
					break;
				case 'accepted':
					$this->redirect('panel/main',3,'Hi '.$this->s['admin']->status['user'].'...');
					break;
				case 'login-error':
					$data['notice'] = '<div class="alert">Wrong username or password</div>';
					break;
				case 'wait':
					$data['notice'] = '<div class="alert">You have to wait 5 seconds before you can try to login another time</div>';
					break;
			}
		}
		$this->set($data);
		$this->load('login');
	}
	
	public function logout()
	{
		$res = $this->s['admin']->logout();
		if ($res == 'not-logged') {
			$data['notice'] = "<div class='alert'>You can't logout because you are not logged..</div>\n";

		} else if ($res == 'was-logged') {
			$data['notice'] = "<div class='executed'>Logout executed successfully!</div>\n";

		} else if ($res == 'error') {

		}

		$data['login'] = Url::getRoot('adminusers/login');
		$this->set($data);
		$this->load('logout');
	}

	public function forceout($id)
	{
		$this->shift(1);
		
		$this->s['admin']->check();
		$data['menù'] = $this->h['Menu']->render('back');
		$data['notice'] = null;
		$id = (int)$id;
		if (strcmp($this->s['admin']->status['id_user'],$id) !== 0)
			{
			if ($this->s['admin']->forceOut($id))
			{
				$data['notice'] = "<div class='executed'>User has been forced out..</div>\n";
			}
			else
			{
				$data['notice'] = "<div class='alert'>Error..</div>\n";
			}
		}
		
		$this->set($data);
		$this->load('forceout');
	}

	public function main()
	{
		$this->shift();

		$this->s['admin']->check();
		
		if (!$this->s['admin']->checkCsrf($this->viewArgs['token'])) $this->redirect('panel/main/',2,'wrong token..');
		
		$this->loadScaffold('main',array('popup'=>false));
		$this->scaffold->setWhereQueryClause(array('id_group'=>$this->viewArgs['id_group']));
		$this->scaffold->loadMain('adminusers:id_user,adminusers:username','adminusers:id_user','edit,del');
		$this->scaffold->addItem('simpleLink','adminusers/forceout/;adminusers:id_user;',null,'ForceOut');
		$this->scaffold->update('del');
		$data['scaffold'] = $this->scaffold->render();
		$this->set($data);
		$this->load('main');
	}

	public function form($queryType = 'insert')
	{
		$this->shift(1);

		$this->s['admin']->check();
		
		if (!$this->s['admin']->checkCsrf($this->viewArgs['token'])) $this->redirect('panel/main/',2,'wrong token..');
		
		$this->m['AdminusersModel']->updateTable('insert,update');

		$this->loadScaffold('form');
		$this->scaffold->loadForm($queryType,"adminusers/form/$queryType");
		$this->scaffold->getFormValues('sanitizeHtml');
		$this->scaffold->setFormEntries('model',array('password'=>'Password'));
		$this->scaffold->form->setEntry('confirmation','Password');
		$data['scaffold'] = $this->scaffold->render(null,'username,password,confirmation,id_user');
		
		$this->set($data);
		$this->load('main');
	}

// 	public function associate()
// 	{
// 		$this->shift(0);
// 		
// 		$this->s['admin']->check();
// 		
// 		if (!$this->s['admin']->checkCsrf($this->viewArgs['token'])) $this->redirect('panel/main/',2,'wrong token..');
// 		
// 		$this->m['AdminusersModel']->printAssError = 'yes';
// 		$this->m['AdminusersModel']->updateTable('associate,dissociate');
// 
// 		$data['notice'] = $this->m['AdminusersModel']->notice;
// 
// 		$data['menu'] = $this->h['Menu']->render('back');
// 
// 		$data['action'] = $this->baseUrl.'/adminusers/associate'.$this->viewStatus;
// 
// 		$data['groups'] = $this->m['AdminusersModel']->getFieldArray('admingroups:id_group','admingroups:name');
// 
// 		//get the name of the user whose id is $_POST['id_user']
// 		$users = $this->m['AdminusersModel']->db->select('adminusers','username','id_user='.(int)$_POST['id_user']);
// 		$data['user'] = $users[0]['adminusers']['username'];
// 		
// 		//get the groups inside which the user is inserted
// 		$this->m['AdminusersModel']->setWhereQueryClause(array('id_user'=>(int)$_POST['id_user']));
// 		$this->m['AdminusersModel']->orderBy = 'admingroups.id_group desc';
// 		$data['groupsUser'] = $this->m['AdminusersModel']->getAll('Boxes');
// // 		echo $this->m['AdminusersModel']->getQuery();
// 		
// 		$this->set($data);
// 		$this->load('associate');
// 	}

}