-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
177 lines (154 loc) · 6.23 KB
/
index.php
File metadata and controls
177 lines (154 loc) · 6.23 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
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
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json;charset=UTF-8');
header('Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE');
header('Access-Control-Max-Age: 3600');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
require_once 'util/Route.php';
require_once 'config/Constants.php';
require_once 'util/PermissionHandler.php';
require_once 'controller/AuthController.php';
require_once 'controller/AuthorController.php';
require_once 'controller/BookController.php';
require_once 'controller/UserController.php';
// Route::add($request_method, $request_path, $callback_function, $enable_auth = true)
/*
*
* AUTHENTICATION ROUTES
*
*/
// LOGIN
if(Constants::JWT['TOKEN_AUTHENTICATION']) {
Route::add('POST', '/api/auth/login/?', function (Request $request) {
echo (new AuthController())->login($request->getJSON());
}, false);
}
/*
*
* USER ROUTES
*
*/
// LIST ALL USERS AND LIST USER BY ID
Route::add('GET', '/api/(?P<resource>user)(/(?P<id>[0-9]+))?/?', function (Request $request) {
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'view',$request->params['id']??null)) {
echo (!isset($request->params['id']))?
(new UserController())->findAll():
(new UserController())->findById($request->params['id']);
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});
// CREATE USER
Route::add('POST', '/api/user/?', function (Request $request) {
echo (new UserController())->create($request->getJSON());
}, false);
// UPDATE A USER
Route::add('PUT', '/api/(?P<resource>user)/?', function (Request $request) {
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'update')) {
echo (new UserController())->update($request->getJSON());
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});
// DELETE USER BY ID
Route::add('DELETE', '/api/(?P<resource>user)/(?P<id>[0-9]+)', function (Request $request) {
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'delete', $request->params['id'])) {
echo (new UserController())->delete($request->params['id']);
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});
/*
*
* BOOK ROUTES
*
*/
// LIST ALL BOOKS AND LIST A BOOK BY ID
Route::add('GET', '/api/(?P<resource>book)(/(?P<id>[0-9]+))?/?', function (Request $request) {
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'view',$request->params['id']??null)) {
echo (!isset($request->params['id']))?
(new BookController())->findAll():
(new BookController())->findById($request->params['id']);
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});
// CREATE A NEW BOOK
Route::add('POST', '/api/(?P<resource>book)/?', function (Request $request) {
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'create')) {
echo (new BookController())->create($request->getJSON());
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});
// UPDATE A BOOK
Route::add('PUT', '/api/(?P<resource>book)/?', function (Request $request) {
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'update')) {
echo (new BookController())->update($request->getJSON());
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});
// DELETE A BOOK BY ID
Route::add('DELETE', '/api/(?P<resource>book)/(?P<id>[0-9]+)', function (Request $request) {
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'delete', $request->params['id'])) {
echo (new BookController())->delete($request->params['id']);
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});
/*
*
* AUTHOR ROUTES
*
*/
// LIST ALL AUTHORS AND LIST A AUTHOR BY ID
Route::add('GET', '/api/(?P<resource>author)(/(?P<id>[0-9]+))?/?', function (Request $request) {
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'view',$request->params['id']??null)) {
echo (!isset($request->params['id']))?
(new AuthorController())->findAll():
(new AuthorController())->findById($request->params['id']);
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});
// CREATE A NEW AUTHOR
Route::add('POST', '/api/(?P<resource>author)/?', function (Request $request) {
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'create')) {
echo (new AuthorController())->create($request->getJSON());
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});
// UPDATE A AUTHOR
Route::add('PUT', '/api/(?P<resource>author)/?', function (Request $request) {
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'update')) {
echo (new AuthorController())->update($request->getJSON());
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});
// DELETE A AUTHOR BY ID
Route::add('DELETE', '/api/(?P<resource>author)/(?P<id>[0-9]+)', function (Request $request) {
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'delete', $request->params['id'])) {
echo (new AuthorController())->delete($request->params['id']);
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});
Route::run();
// DELETE USER BY ID OR ALL USERS
/*Route::add('DELETE', '/api/(?P<resource>user)/?', function (Request $request) {
//check if has a resource_id
$resource_id = $request->getJSON()->id ?? null;
if (PermissionHandler::hasPermissionOn($request->params['resource'], 'delete', $resource_id)) {
echo (new UserController())->delete($request->getJSON());
} else {
echo Response::sendWithCode(403, Constants::MSG['PERMISSION_DENY']);
}
});*/
// LIST A BOOK BY TITLE
/*Route::add('GET', '/api/book/([a-zA-Z0-9]+)', function (Request $request) {
echo (new BookController())->findByTitle($request->params['resource']);
});
*/