-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionController.php
More file actions
151 lines (126 loc) · 4.53 KB
/
SessionController.php
File metadata and controls
151 lines (126 loc) · 4.53 KB
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
<?php
use JSONms\Controllers\RestfulController;
use GuzzleHttp\Client as GuzzleClient;
class SessionController extends RestfulController {
private function getDemoStructure() {
$stmt = $this->query('get-demo-structure');
if ($stmt->rowCount() > 0) {
$rows = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($rows as $row) {
$row->permission_admin = [];
$row->permission_structure = [];
$row->type = 'structure,admin';
return $row;
}
}
return null;
}
private function getEndpoints($userId) {
$stmt = $this->query('get-all-endpoints', [
'userId' => $userId,
]);
if ($stmt->rowCount() > 0) {
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
return [];
}
private function getLoginUrl() {
$httpClient = new GuzzleClient([
'timeout' => 3.0,
'connect_timeout' => 3.0,
]);
try {
// Google Client Configuration
$client = new Google_Client();
$client->setHttpClient($httpClient);
$client->setClientId($_ENV['GOOGLE_OAUTH_CLIENT_ID']);
$client->setClientSecret($_ENV['GOOGLE_OAUTH_CLIENT_SECRET']);
$client->setRedirectUri($_ENV['GOOGLE_OAUTH_CALLBACK_URL']);
$client->addScope('email');
$client->addScope('profile');
return $client->createAuthUrl();
} catch(\Exception $e) {
throwError(500, $e->getMessage());
}
}
public function indexAction() {
$loggedIn = isset($_SESSION['access_token']) && $_SESSION['access_token'];
$user = null;
$loginUrl = null;
$structures = [];
$endpoints = [];
// Fetch demo structure
$demo = $this->getDemoStructure();
if ($demo) {
$structures[] = $demo;
}
if ($loggedIn) {
// Check if user already exists
$stmt = $this->query('get-user-by-id', [
'id' => $this->getCurrentUserId(),
]);
if ($stmt->rowCount() > 0) {
// User exists, fetch data
$user = $stmt->fetch(PDO::FETCH_OBJ);
} else {
$loginUrl = $this->getLoginUrl();
}
}
else {
$loginUrl = $this->getLoginUrl();
}
if ($loggedIn && isset($user)) {
// Fetch all structures
$stmt = $this->query('get-all-structures', [
'userId' => $this->getCurrentUserId(),
]);
$structures = [];
if ($stmt->rowCount() > 0) {
$rows = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($rows as $row) {
$row->permission_admin = array_filter(explode(',', $row->permission_admin ?? ''));
$row->permission_structure = array_filter(explode(',', $row->permission_structure ?? ''));
$structures[] = $row;
}
}
}
// Fetch demo structure
$loggedIn = $loggedIn && isset($user);
if ($loggedIn) {
$endpoints = $this->getEndpoints($user->id);
}
$this->responseJson([
'loggedIn' => $loggedIn,
'user' => $user,
'googleOAuthSignInUrl' => $loginUrl,
'structures' => $structures,
'endpoints' => $endpoints,
]);
}
public function logoutAction() {
session_destroy();
// Google Client Configuration
$client = new Google_Client();
$client->setClientId($_ENV['GOOGLE_OAUTH_CLIENT_ID']);
$client->setClientSecret($_ENV['GOOGLE_OAUTH_CLIENT_SECRET']);
$client->setRedirectUri($_ENV['GOOGLE_OAUTH_CALLBACK_URL']);
$client->addScope('email');
$client->addScope('profile');
// Generate the login URL
$loginUrl = $client->createAuthUrl();
// Fetch demo structure
$structures = [];
$demo = $this->getDemoStructure();
if ($demo) {
$structures[] = $demo;
}
// Return the JSON response
$this->responseJson([
'loggedIn' => false,
'user' => null,
'googleOAuthSignInUrl' => $loginUrl,
'structures' => $structures,
'endpoints' => [],
]);
}
}