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
|
|
|
|
13
|
|
|
class WorkspaceController implements ControllerProviderInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
private $app; |
17
|
|
|
|
18
|
|
|
public function connect(Application $app) |
19
|
|
|
{ |
20
|
|
|
$this->app = $app; |
21
|
|
|
$factory = $app['controllers_factory']; |
22
|
|
|
# il mount point e' precedente e non serve prima |
23
|
|
|
$this->app['db']; |
24
|
|
|
R::fancyDebug( TRUE ); |
25
|
|
|
$factory->get('/', array($this, 'getWorkspaceList')); |
26
|
|
|
$factory->post('/', array($this, 'createWorkspace')); |
27
|
|
|
$factory->get('/{id}', array($this, 'getWorkspace')); |
28
|
|
|
$factory->get('/{id}/share', array($this, 'share')); |
29
|
|
|
$factory->post('/{id}/part', array($this, 'postPart')); |
30
|
|
|
return $factory; |
31
|
|
|
} |
32
|
|
|
public function getSessionId(){ |
33
|
|
|
$user_id=$this->app['session']->get('user')['id']; |
34
|
|
|
return $user_id; |
35
|
|
|
} |
36
|
|
|
public function getWorkspaceList(Request $request) |
37
|
|
|
{ |
38
|
|
|
$user_id=$this->getSessionId(); |
39
|
|
|
$workspaces = R::getAll("SELECT ws.id, |
40
|
|
|
ws.title, |
41
|
|
|
ws.description, |
42
|
|
|
ws.environment, |
43
|
|
|
ws.completed |
44
|
|
|
FROM userworkspace AS uws |
45
|
|
|
LEFT JOIN workspace AS ws |
46
|
|
|
ON uws.workspace = ws.id |
47
|
|
|
WHERE uws.user = ?",[$user_id]); |
48
|
|
|
$list=[]; |
49
|
|
|
foreach($workspaces as $ws){ |
50
|
|
|
array_push($list,[ |
51
|
|
|
"id"=>$ws['id'], |
52
|
|
|
"title"=>$ws['title'], |
53
|
|
|
"description"=>$ws['description'], |
54
|
|
|
"environment"=>$ws['environment'], |
55
|
|
|
"point"=>0,//TODO fare una view con i point già calcolati per il ws |
56
|
|
|
"completed"=>$ws['completed'], |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
$headers = []; |
60
|
|
|
return JsonResponse::create($list, 200, $headers)->setSharedMaxAge(300); |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
public function createWorkspace(Request $request) |
64
|
|
|
{ |
65
|
|
|
$user_id=$this->getSessionId(); |
66
|
|
|
$counter=0; |
|
|
|
|
67
|
|
|
$data = json_decode($request->getContent(), true); |
68
|
|
|
//TODO validate json_decode |
69
|
|
|
$title=$data['title']; |
70
|
|
|
$description=$data['description']; |
71
|
|
|
$environment=$data['environment']; |
72
|
|
|
|
73
|
|
|
$patrol = $data['team']['patrol']; |
74
|
|
|
$unit = $data['team']['unit']; |
75
|
|
|
$group = $data['team']['group']; |
76
|
|
|
|
77
|
|
|
//save the workspace get id |
78
|
|
|
$ws = R::dispense("workspace"); |
79
|
|
|
$ws->title=$title; |
80
|
|
|
$ws->description=$description; |
81
|
|
|
$ws->environment=$environment; |
82
|
|
|
$ws->completed=false; |
83
|
|
|
$ws->inserttime=date('Y-m-d H:i:s'); |
84
|
|
|
$ws->lastupdatetime=date('Y-m-d H:i:s'); |
85
|
|
|
$id = R::store($ws); |
86
|
|
|
|
87
|
|
|
//save the team |
88
|
|
|
$team = R::dispense("team"); |
89
|
|
|
$team->workspace=$id; |
90
|
|
|
$team->patrol=$patrol; |
91
|
|
|
$team->unit=$unit; |
92
|
|
|
$team->group=$group; |
93
|
|
|
$team_id = R::store($team); |
|
|
|
|
94
|
|
|
|
95
|
|
|
//create a phantom part to add badge |
96
|
|
|
$part = R::dispense("part"); |
97
|
|
|
$part->workspace=$id; |
98
|
|
|
$part->user=$user_id; |
99
|
|
|
$part->inserttime=date('Y-m-d H:i:s'); |
100
|
|
|
$part->lastupdatetime=date('Y-m-d H:i:s'); |
101
|
|
|
$part->totalpoint=0; |
102
|
|
|
$part_id = R::store($part); |
103
|
|
|
|
104
|
|
|
//add the badge to the project |
105
|
|
|
foreach($data['badges'] as $badge_id){ |
106
|
|
|
//TODO insert those badge as first hidden post |
107
|
|
|
$pb = R::dispense("partbadge"); |
108
|
|
|
$pb->badge=$badge_id; |
109
|
|
|
$pb->part=$part_id; |
110
|
|
|
$pb->inserttime=date('Y-m-d H:i:s'); |
111
|
|
|
$tmp = R::store($pb); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
//add the workspace created to the user as owner |
115
|
|
|
$usw = R::dispense("userworkspace"); |
116
|
|
|
$usw->user=$user_id; |
117
|
|
|
$usw->workspace=$id; |
118
|
|
|
$usw->inserttime=date('Y-m-d H:i:s'); |
119
|
|
|
R::store($usw); |
120
|
|
|
|
121
|
|
|
$res = ["id" => $id]; |
122
|
|
|
$headers = []; |
123
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getWorkspace($id,Request $request){ |
127
|
|
|
$user_id=$this->getSessionId(); |
|
|
|
|
128
|
|
|
//TODO controllare che l'utente abbia diritto a vedere questo workspace |
129
|
|
|
|
130
|
|
|
$workspace = R::findOne("workspace","id = ?",[$id]); |
131
|
|
|
$part = R::findAll("part","workspace = ?",[$id]); |
132
|
|
|
|
133
|
|
|
$badges = R::findAll("workspacebadge","workspace = ?",[$id]); |
134
|
|
|
|
135
|
|
|
$l_part=[]; |
136
|
|
|
foreach($part as $p){ |
137
|
|
|
array_push($l_part,intval($p['id'])); |
138
|
|
|
} |
139
|
|
|
$l_badges=[]; |
140
|
|
|
foreach($badges as $b){ |
141
|
|
|
array_push($l_badges,intval($b['badge'])); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$res = [ |
145
|
|
|
'id'=> $workspace['id'], |
146
|
|
|
'title'=> $workspace['title'], |
147
|
|
|
'description'=> $workspace['description'], |
148
|
|
|
'environment'=> $workspace['environment'], |
149
|
|
|
'environment'=> $workspace['environment'], |
150
|
|
|
'completed'=> $workspace['completed'], |
151
|
|
|
'parts'=>$l_part, |
152
|
|
|
'badges'=>$l_badges |
153
|
|
|
]; |
154
|
|
|
$headers = []; |
155
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function share($id,Request $request){ |
159
|
|
|
$generatedKey = hash("sha256",(mt_rand(10000,99999).time().$id)); |
160
|
|
|
//TODO verificare documentazione realtiva sulla reale entropia generata da questo sistema |
161
|
|
|
$user_id=$this->getSessionId(); |
162
|
|
|
$share = R::dispense("share"); |
163
|
|
|
$share->user=$user_id; |
164
|
|
|
$share->workspace=$id; |
165
|
|
|
$share->key=$generatedKey; |
166
|
|
|
$share->inserttime=date('Y-m-d H:i:s'); |
167
|
|
|
$share_id = R::store($share); |
168
|
|
|
|
169
|
|
|
$date = new \DateTime(); |
170
|
|
|
date_add($date, date_interval_create_from_date_string('15 minutes')); |
171
|
|
|
|
172
|
|
|
$res = [ |
173
|
|
|
"id"=>$share_id, |
174
|
|
|
"key"=>$generatedKey, |
175
|
|
|
"expire"=>$date->format('Y-m-d H:i:s') |
176
|
|
|
]; |
177
|
|
|
|
178
|
|
|
$headers = []; |
179
|
|
|
return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function postPart($id,Request $request){ |
183
|
|
|
$user_id=$this->getSessionId(); |
|
|
|
|
184
|
|
|
|
185
|
|
|
$res = []; |
186
|
|
|
$headers = []; |
187
|
|
|
return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.