This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.sqlAuthDriver.php
215 lines (194 loc) · 8.86 KB
/
class.sqlAuthDriver.php
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
defined('AJXP_EXEC') or die( 'Access not allowed');
/**
* Store authentication data in an SQL database
* @package AjaXplorer_Plugins
* @subpackage Auth
*/
class sqlAuthDriver extends AbstractAuthDriver
{
public $sqlDriver;
public $driverName = "sql";
public function init($options)
{
parent::init($options);
require_once(AJXP_BIN_FOLDER."/dibi.compact.php");
$this->sqlDriver = AJXP_Utils::cleanDibiDriverParameters($options["SQL_DRIVER"]);
try {
dibi::connect($this->sqlDriver);
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
exit(1);
}
}
public function performChecks()
{
if(!isSet($this->options)) return;
$test = AJXP_Utils::cleanDibiDriverParameters($this->options["SQL_DRIVER"]);
if (!count($test)) {
throw new Exception("You probably did something wrong! To fix this issue you have to remove the file \"bootstrap.json\" and rename the backup file \"bootstrap.json.bak\" into \"bootsrap.json\" in data/plugins/boot.conf/");
}
}
public function supportsUsersPagination()
{
return true;
}
// $baseGroup = "/"
public function listUsersPaginated($baseGroup, $regexp, $offset, $limit, $recursive = true)
{
$ignoreHiddens = "NOT EXISTS (SELECT * FROM [ajxp_user_rights] AS c WHERE [c.login]=[u.login] AND [c.repo_uuid] = 'ajxp.hidden')";
if($recursive){
$groupPathCondition = "[groupPath] LIKE %like~";
}else{
$groupPathCondition = "[groupPath] = %s";
}
if ($regexp != null) {
$res = dibi::query("SELECT * FROM [ajxp_users] AS u WHERE [login] ".AJXP_Utils::regexpToLike($regexp)." AND $groupPathCondition AND $ignoreHiddens ORDER BY [login] ASC %lmt %ofs", AJXP_Utils::cleanRegexp($regexp), $baseGroup, $limit, $offset) ;
} else if ($offset != -1 || $limit != -1) {
$res = dibi::query("SELECT * FROM [ajxp_users] AS u WHERE $groupPathCondition AND $ignoreHiddens ORDER BY [login] ASC %lmt %ofs", $baseGroup, $limit, $offset);
} else {
$res = dibi::query("SELECT * FROM [ajxp_users] AS u WHERE $groupPathCondition AND $ignoreHiddens ORDER BY [login] ASC", $baseGroup);
}
$pairs = $res->fetchPairs('login', 'password');
return $pairs;
}
public function findUserPage($baseGroup, $userLogin, $usersPerPage, $offset){
$res = dibi::query("SELECT COUNT(*) FROM [ajxp_users] WHERE [login] <= %s", $userLogin);
$count = $res->fetchSingle();
return ceil(($count - $offset) / $usersPerPage) - 1;
}
public function getUsersCount($baseGroup = "/", $regexp = "", $filterProperty = null, $filterValue = null, $recursive = true)
{
// WITH PARENT
// SELECT COUNT(*) FROM ajxp_users AS u, ajxp_user_rights AS r WHERE u.groupPath LIKE '/%' AND r.login=u.login AND r.repo_uuid = 'ajxp.parent_user'
// WITH SPECIFIC PARENT 'username'
// SELECT COUNT(*) FROM ajxp_users AS u, ajxp_user_rights AS r WHERE u.groupPath LIKE '/%' AND r.login=u.login AND r.repo_uuid = 'ajxp.parent_user' AND r.rights = 'username'
// WITHOUT PARENT
// SELECT COUNT(*) FROM ajxp_users AS u WHERE NOT EXISTS (SELECT * FROM ajxp_user_rights AS c WHERE c.login=u.login AND c.repo_uuid='ajxp.parent_user')
$ands = array();
$select = "SELECT COUNT(*) FROM [ajxp_users] AS u WHERE %and";
if(!empty($regexp)){
$ands[] = array("[u.login] ".AJXP_Utils::regexpToLike($regexp), AJXP_Utils::cleanRegexp($regexp));
}
if($recursive){
$ands[] = array("[u.groupPath] LIKE %like~", $baseGroup);
}else{
$ands[] = array("[u.groupPath] = %s", $baseGroup);
}
$ands[] = array("NOT EXISTS (SELECT * FROM [ajxp_user_rights] AS c WHERE [c.login]=[u.login] AND [c.repo_uuid] = 'ajxp.hidden')");
if($filterProperty !== null && $filterValue !== null){
if($filterProperty == "parent"){
$filterProperty = "ajxp.parent_user";
}else if($filterProperty == "admin"){
$filterProperty = "ajxp.admin";
}
if($filterValue == AJXP_FILTER_EMPTY){
$ands[] = array("NOT EXISTS (SELECT * FROM [ajxp_user_rights] AS c WHERE [c.login]=[u.login] AND [c.repo_uuid] = %s)",$filterProperty);
}else if($filterValue == AJXP_FILTER_NOT_EMPTY){
$select = "SELECT COUNT(*) FROM [ajxp_users] AS u, [ajxp_user_rights] AS r WHERE %and";
$ands[] = array("[r.login]=[u.login]");
$ands[] = array("[r.repo_uuid] = %s", $filterProperty);
}else{
$select = "SELECT COUNT(*) FROM [ajxp_users] AS u, [ajxp_user_rights] AS r WHERE %and";
$ands[] = array("[r.login]=[u.login]");
$ands[] = array("[r.repo_uuid] = %s", $filterProperty);
$ands[] = array("[r.rights] ".AJXP_Utils::likeToLike($filterValue), AJXP_Utils::cleanLike($filterValue));
}
}
$res = dibi::query($select, $ands);
return $res->fetchSingle();
}
public function listUsers($baseGroup="/")
{
$pairs = array();
$ignoreHiddens = "NOT EXISTS (SELECT * FROM [ajxp_user_rights] AS c WHERE [c.login]=[u.login] AND [c.repo_uuid] = 'ajxp.hidden')";
$res = dibi::query("SELECT * FROM [ajxp_users] as u WHERE [u.groupPath] LIKE %like~ AND $ignoreHiddens ORDER BY [u.login] ASC", $baseGroup);
$rows = $res->fetchAll();
foreach ($rows as $row) {
$grp = $row["groupPath"];
if(strlen($grp) > strlen($baseGroup)) continue;
$pairs[$row["login"]] = $row["password"];
}
return $pairs;
}
public function userExists($login)
{
$res = dibi::query("SELECT COUNT(*) FROM [ajxp_users] WHERE [login]=%s", $login);
return (intval($res->fetchSingle()) > 0);
}
public function checkPassword($login, $pass, $seed)
{
$userStoredPass = $this->getUserPass($login);
if(!$userStoredPass) return false;
if ($this->getOption("TRANSMIT_CLEAR_PASS") === true) { // Seed = -1 means that password is not encoded.
return AJXP_Utils::pbkdf2_validate_password($pass, $userStoredPass); //($userStoredPass == md5($pass));
} else {
return (md5($userStoredPass.$seed) == $pass);
}
}
public function usersEditable()
{
return true;
}
public function passwordsEditable()
{
return true;
}
public function createUser($login, $passwd)
{
if($this->userExists($login)) return "exists";
$userData = array("login" => $login);
if ($this->getOption("TRANSMIT_CLEAR_PASS") === true) {
$userData["password"] = AJXP_Utils::pbkdf2_create_hash($passwd); //md5($passwd);
} else {
$userData["password"] = $passwd;
}
$userData['groupPath'] = '/';
dibi::query('INSERT INTO [ajxp_users]', $userData);
}
public function changePassword($login, $newPass)
{
if(!$this->userExists($login)) throw new Exception("User does not exists!");
$userData = array("login" => $login);
if ($this->getOption("TRANSMIT_CLEAR_PASS") === true) {
$userData["password"] = AJXP_Utils::pbkdf2_create_hash($newPass); //md5($newPass);
} else {
$userData["password"] = $newPass;
}
dibi::query("UPDATE [ajxp_users] SET ", $userData, "WHERE [login]=%s", $login);
}
public function deleteUser($login)
{
dibi::query("DELETE FROM [ajxp_users] WHERE [login]=%s", $login);
}
public function getUserPass($login)
{
$res = dibi::query("SELECT [password] FROM [ajxp_users] WHERE [login]=%s", $login);
$pass = $res->fetchSingle();
return $pass;
}
public function installSQLTables($param)
{
$p = AJXP_Utils::cleanDibiDriverParameters($param["SQL_DRIVER"]);
return AJXP_Utils::runCreateTablesQuery($p, $this->getBaseDir()."/create.sql");
}
}