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
|
<?php
/*
* studip_user.php - Basisklasse für Stud.IP User
*
* Copyright (C) 2006 - Marcus Lunzenauer <mlunzena@uos.de>
*
* This program 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 2 of
* the License, or (at your option) any later version.
*/
/**
* <ClassDescription>
*
* @package studip
* @subpackage base
*
* @author mlunzena
* @copyright (c) Authors
*
* @todo using UserManagement class for now, shall be removed afterwards
*/
class Studip_User {
// internal variables
var $id;
var $user_name;
var $first_name;
var $last_name;
var $email;
var $permission;
var $fullname;
var $auth_plugin;
var $visibility;
var $error;
// Constructor
function __construct($user) {
$fields = self::get_fields();
foreach ($fields as $field) {
if (isset($user[$field])) {
$this->$field = $user[$field];
}
}
if (isset($this->id)) {
$this->fullname = get_fullname($this->id);
}
}
/**
* @return bool
*/
function save()
{
foreach (self::get_fields() as $v => $k) {
if (isset($this->$k)) {
$user[$v] = $this->$k;
}
}
// no id, create
if (!$this->id) {
$user_management = new UserManagement();
if (!$user_management->createNewUser($user)) {
$this->error = $user_management->msg; // TODO
return FALSE;
}
// set id
$this->id = $user_management->user_data['auth_user_md5.user_id'];
} else {
// update
$user_management = new UserManagement($this->id);
if (!$user_management->changeUser($user)) {
$this->error = $user_management->msg; // TODO
return FALSE;
}
}
return TRUE;
}
/**
* @return bool
*/
function destroy()
{
$user_management = new UserManagement($this->id);
if (!$user_management->deleteUser()) {
$this->error = $user_management->msg; // TODO
return FALSE;
}
return TRUE;
}
/**
* <MethodDescription>
*
* @param type <description>
*
* @return mixed <description>
*/
public static function find_by_user_name($user_name)
{
$db = DBManager::get();
$stmt = $db->prepare('SELECT * FROM auth_user_md5 WHERE username = ?');
$stmt->execute([$user_name]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$result = NULL;
if ($row) {
$user = [];
foreach (self::get_fields() as $old => $new) {
$tmp = explode('.', $old);
$user[$new] = $row[array_pop($tmp)];
}
$result = new Studip_User($user);
}
return $result;
}
/**
* <MethodDescription>
*
* @param type <description>
*
* @return mixed <description>
*/
public static function find_by_user_id($user_id)
{
$db = DBManager::get();
$stmt = $db->prepare('SELECT * FROM auth_user_md5 WHERE user_id = ?');
$stmt->execute([$user_id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$result = NULL;
if ($row) {
$user = [];
foreach (self::get_fields() as $old => $new) {
$tmp = explode('.', $old);
$user[$new] = $row[array_pop($tmp)];
}
$result = new Studip_User($user);
}
return $result;
}
/**
* <MethodDescription>
*
* @param type <description>
*
* @return mixed <description>
*/
public static function find_by_status($status)
{
$db = DBManager::get();
$stmt = $db->prepare('SELECT username FROM auth_user_md5 WHERE perms = ?');
$stmt->execute([$status]);
return $stmt->fetchAll(PDO::FETCH_COLUMN);
}
/**
* <MethodDescription>
*
* @return array <description>
*/
public static function get_fields()
{
$fields = ['auth_user_md5.user_id' => 'id',
'auth_user_md5.username' => 'user_name',
'auth_user_md5.Vorname' => 'first_name',
'auth_user_md5.Nachname' => 'last_name',
'auth_user_md5.Email' => 'email',
'auth_user_md5.perms' => 'permission',
'auth_user_md5.auth_plugin' => 'auth_plugin',
'auth_user_md5.visible' => 'visibility'];
return $fields;
}
}
|