-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiRegistrationController.php
100 lines (75 loc) · 3.76 KB
/
ApiRegistrationController.php
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
<?php namespace App\Http\Controllers;
use Session;
use Request;
use DB;
use CRUDBooster;
class ApiRegistrationController extends \crocodicstudio\crudbooster\controllers\ApiController {
function __construct() {
$this->table = "users";
$this->permalink = "registration";
$this->method_type = "post";
}
public function hook_before(&$postdata) {
//This method will be execute before run the main process
$emails_from_db = DB::select('select email from users');
$emails = [];
foreach ( $emails_from_db as $email_from_db )
{
$emails[] = $email_from_db->email;
}
$usernames_from_db = DB::select('select username from users');
$usernames = [];
foreach ( $usernames_from_db as $username_from_db )
{
$usernames[] = $username_from_db->username;
}
$tel_numbers_from_db = DB::select('select tel_number from users');
$tel_numbers = [];
foreach ( $tel_numbers_from_db as $tel_number_from_db )
{
$tel_numbers[] = $tel_number_from_db->tel_number;
}
$response = [
'api_status' => 0,
'api_message' => 'Ошибка!',
'errors' => []
];
If ( array_search($postdata['email'], $emails) !== false AND array_search($postdata['username'], $usernames) !== false AND array_search($postdata['tel_number'], $tel_numbers) !== false ) {
$response['errors']['email'] = 'Пользователь с таким Email уже существует!';
$response['errors']['username'] = 'Пользователь с таким Username уже существует!';
$response['errors']['tel_number'] = 'Пользователь с таким Телефоном уже существует!!';
$resp = response()->json($response);
$resp->send();
exit;
} else if ( array_search($postdata['email'], $emails) !== false ) {
$response['errors']['email'] = 'Пользователь с таким Email уже существует!';
$resp = response()->json($response);
$resp->send();
exit;
} else if ( array_search($postdata['username'], $usernames) !== false ) {
$response['errors']['username'] = 'Пользователь с таким Username уже существует!';
$resp = response()->json($response);
$resp->send();
exit;
} else if ( array_search($postdata['tel_number'], $tel_numbers) !== false ) {
$response['errors']['tel_number'] = 'Пользователь с таким Телефоном уже существует!!';
$resp = response()->json($response);
$resp->send();
exit;
} else {
}
}
public function hook_query(&$query) {
}
public function hook_after($postdata,&$result) {
//This method will be execute after run the main process
$auth_token = openssl_random_pseudo_bytes(20);
$auth_token = bin2hex($auth_token);
DB::update('update users set auth_key = "' .$auth_token. '" where id = "'.$result['id'].'" ');
unset($result['id']);
If ( $result['api_status'] === 0 ) {
} else {
$result['auth_token'] = $auth_token;
}
}
}