1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitPrepared\Bundle\D1b0Workspace\Controller\V1; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
8
|
|
|
use Silex\Application; |
9
|
|
|
use Silex\Api\ControllerProviderInterface; |
10
|
|
|
use Monolog\Logger; |
11
|
|
|
use RedBeanPHP\Facade as R; |
12
|
|
|
use BitPrepared\Bundle\D1b0Workspace\Exception\UnauthorizedException; |
13
|
|
|
|
14
|
|
|
class UserController implements ControllerProviderInterface |
15
|
|
|
{ |
16
|
|
|
public $DATE_FORMAT = 'Y-m-d\TH:i:s\Z'; |
17
|
|
|
|
18
|
|
|
private $app; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
public function connect(Application $app) |
22
|
|
|
{ |
23
|
|
|
$this->app = $app; |
24
|
|
|
$factory = $app['controllers_factory']; |
25
|
|
|
# il mount point e' precedente e non serve prima |
26
|
|
|
$this->app['db']; |
27
|
|
|
//R::fancyDebug( TRUE ); |
|
|
|
|
28
|
|
|
$factory->post('/signup', array($this, 'signup')); |
29
|
|
|
$factory->get('/{id}', array($this, 'get'))->before([$this, 'isSession']); |
30
|
|
|
$factory->post('/{id}/badge', array($this, 'postBadge'))->before([$this, 'isSession']); |
31
|
|
|
$factory->get('/{id}/badge/{id_badge}', array($this, 'getBadge'))->before([$this, 'isSession']); |
32
|
|
|
$factory->patch('/{id}/badge/{id_badge}/completed', array($this, 'markBadgeAsCompleted'))->before([$this, 'isSession']); |
33
|
|
|
$factory->delete('/{id}/badge/{id_badge}', array($this, 'deleteUserBadge'))->before([$this, 'isSession']); |
34
|
|
|
$factory->get('/{id}/ticket', array($this, 'getTicket'))->before([$this, 'isSession']); |
35
|
|
|
return $factory; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function isSession(Request $request, Application $app) { |
39
|
|
|
if ($this->app['session']->has('user') !== true) { |
40
|
|
|
throw new UnauthorizedException("errore", 1); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function get($id, Request $request) |
45
|
|
|
{ |
46
|
|
|
$user = R::findOne('user', 'id = ?', ["$id"]); |
47
|
|
|
$headers = []; |
48
|
|
|
|
49
|
|
|
$output = [ |
50
|
|
|
'name'=>$user->name, |
51
|
|
|
'surname'=>$user->surname, |
52
|
|
|
'authmode'=>$user->authmode, |
53
|
|
|
'skills'=>'', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$badges = R::findAll('userbadgecomplete', 'WHERE user = ?', [$id]); |
57
|
|
|
$badgeList = []; |
58
|
|
View Code Duplication |
foreach ($badges as $badge) { |
|
|
|
|
59
|
|
|
array_push($badgeList, |
60
|
|
|
[ |
61
|
|
|
'badge'=>[ |
62
|
|
|
'id'=>intval($badge['id']), |
63
|
|
|
'name'=>$badge['name'], |
64
|
|
|
'description'=>$badge['description'], |
65
|
|
|
'img'=>$badge['img'] |
66
|
|
|
], |
67
|
|
|
'clove'=>intval($badge['clove']), |
68
|
|
|
'completed'=>boolval($badge['completed']) |
69
|
|
|
] |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
$output['skills'] = $badgeList; |
73
|
|
|
return JsonResponse::create($output, 200, $headers)->setSharedMaxAge(300); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function signup(Request $request) |
77
|
|
|
{ |
78
|
|
|
//TODO ricordarsi di salvare in log il login ^^ |
79
|
|
|
// $this->app['monolog']->addInfo(sprintf("Required '%s'.", 'status')); |
|
|
|
|
80
|
|
|
$this->app->log('log info', [], Logger::INFO); //grazie al traits <- da trasformare prima in app |
81
|
|
|
$data = json_decode($request->getContent(), true); |
82
|
|
|
//TODO: https://github.com/justinrainbow/json-schema VALIDARE LA ROBA IN INPUT |
83
|
|
|
|
84
|
|
|
$authMode = $data['authMode']; |
85
|
|
|
$id = -1; |
86
|
|
|
if ($authMode === 'Email') { |
87
|
|
|
/* |
|
|
|
|
88
|
|
|
$user = R::dispense('user'); |
89
|
|
|
$user->authMode=$data['authMode']; |
90
|
|
|
$user->name=$data['name']; |
91
|
|
|
$user->surname=$data['surname']; |
92
|
|
|
$user->surname=$data['surname'];*/ |
93
|
|
|
try { |
94
|
|
|
$user = R::dispense('user'); |
95
|
|
|
//$user->import($data); |
|
|
|
|
96
|
|
|
$size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB); |
97
|
|
|
$iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM); |
98
|
|
|
$user->salt = $iv; |
99
|
|
|
$user->pwd = hash("sha256", $iv.$data['password']); |
100
|
|
|
$user->status = "checking"; |
101
|
|
|
//$user->id="11"; |
|
|
|
|
102
|
|
|
$user->name = $data['name']; |
103
|
|
|
$user->email = $data['email']; |
104
|
|
|
$user->surname = $data['surname']; |
105
|
|
|
$user->authmode = $data['authMode']; |
106
|
|
|
$user->inserttime = date($this->DATE_FORMAT); |
107
|
|
|
$user->updatetime = date('Y-m-d G:i:s'); |
108
|
|
|
$id = R::store($user); |
109
|
|
|
$res = (object)["id" => $id]; |
110
|
|
|
}catch (Exception $e) { |
|
|
|
|
111
|
|
|
echo $e; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
}else { |
|
|
|
|
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$headers = []; |
119
|
|
|
//TODO redirect to other page (/security/callback?authMode=Email) |
120
|
|
|
return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function postBadge($id, Request $request) |
124
|
|
|
{ |
125
|
|
|
//TODO valiadre id in funzione della sessione utente (altrimenti chiunque aggiunge badge a chiunque) |
126
|
|
|
$data = json_decode($request->getContent(), true); |
127
|
|
|
|
128
|
|
|
$userbadge = R::dispense('userbadge'); |
129
|
|
|
$userbadge->user = $id; |
130
|
|
|
$userbadge->badge = $data['id']; |
131
|
|
|
$userbadge->inserttime = date($this->DATE_FORMAT); |
132
|
|
|
$userbadge->updatetime = date($this->DATE_FORMAT); |
133
|
|
|
$id = R::store($userbadge); |
134
|
|
|
|
135
|
|
|
$res = (object)["id" => $id]; |
136
|
|
|
$headers = []; |
137
|
|
|
return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getBadge($id, $id_badge, Request $request) |
141
|
|
|
{ |
142
|
|
|
$badge = R::findOne('userbadgecomplete', 'WHERE user = ? AND badge = ?', [$id, $id_badge]); |
143
|
|
|
$res = [ |
144
|
|
|
'badge'=>[ |
145
|
|
|
'id'=>intval($badge['badge']), |
146
|
|
|
'name'=>$badge['name'], |
147
|
|
|
'description'=>$badge['description'], |
148
|
|
|
'img'=>$badge['img'] |
149
|
|
|
], |
150
|
|
|
'clove'=>intval($badge['clove']), |
151
|
|
|
'completed'=>boolval($badge['completed']) |
152
|
|
|
]; |
153
|
|
|
$headers = []; |
154
|
|
|
return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300); |
155
|
|
|
} |
156
|
|
|
public function markBadgeAsCompleted($id, $id_badge, Request $request) { |
157
|
|
|
$userbadge = R::findOne('userbadge', "WHERE user = ? AND badge = ?", [$id, $id_badge]); |
158
|
|
|
$userbadge->user = $id; |
159
|
|
|
$userbadge->badge = $id_badge; |
160
|
|
|
$userbadge->updatetime = date($this->DATE_FORMAT); |
161
|
|
|
$userbadge->completed = 1; |
162
|
|
|
$id = R::store($userbadge); |
|
|
|
|
163
|
|
|
$headers = []; |
|
|
|
|
164
|
|
|
$response = new Response(); |
165
|
|
|
$response->headers->set('Content-Type', 'text/html'); |
166
|
|
|
$response->setStatusCode(Response::HTTP_NO_CONTENT); |
167
|
|
|
$response->setSharedMaxAge(300); |
168
|
|
|
return $response; |
169
|
|
|
} |
170
|
|
|
public function deleteUserBadge($id, $id_badge, Request $request) { |
171
|
|
|
$userbadge = R::load('userbadge', $id_badge); |
172
|
|
|
$userbadge->deleted = 1; |
173
|
|
|
$userbadge->updatetime = date($this->DATE_FORMAT); |
174
|
|
|
$id = R::store($userbadge); |
|
|
|
|
175
|
|
|
$headers = []; |
|
|
|
|
176
|
|
|
$response = new Response(); |
177
|
|
|
$response->headers->set('Content-Type', 'text/html'); |
178
|
|
|
$response->setStatusCode(Response::HTTP_NO_CONTENT); |
179
|
|
|
$response->setSharedMaxAge(300); |
180
|
|
|
return $response; |
181
|
|
|
} |
182
|
|
|
public function getTicket($id, Request $request) { |
183
|
|
|
$ticketRaw = R::findAll('ticket', 'WHERE user = ? AND (NOT status = "closed")', [$id]); |
184
|
|
|
|
185
|
|
|
$tickets = []; |
186
|
|
|
foreach ($ticketRaw as $ticket) { |
187
|
|
|
array_push($tickets, [ |
188
|
|
|
"id"=>intval($ticket['id']), |
189
|
|
|
"message"=>$ticket['message'], |
190
|
|
|
"url"=>$ticket['url'], |
191
|
|
|
"priority"=>$ticket['priority'] |
192
|
|
|
]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$headers = []; |
196
|
|
|
return JsonResponse::create($tickets, 200, $headers)->setSharedMaxAge(300); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.