|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controllers\Ajax; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\UserModel; |
|
6
|
|
|
use Core\AjaxController; |
|
7
|
|
|
use Core\Container; |
|
8
|
|
|
use Core\JsonException; |
|
9
|
|
|
use Core\Traits\StringFunctions; |
|
10
|
|
|
|
|
11
|
|
|
class User extends AjaxController{ |
|
12
|
|
|
|
|
13
|
|
|
use StringFunctions; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
private $userModel; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Container $container) |
|
18
|
|
|
{ |
|
19
|
|
|
parent::__construct($container); |
|
20
|
|
|
$this->userModel = new UserModel($this->container); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function isEmailUnique($get) |
|
24
|
|
|
{ |
|
25
|
|
|
//the router needs a parameter with get functions else throsw a wobbly |
|
26
|
|
|
//we pass a get variable and call the /controller/function/get?bla |
|
27
|
|
|
//for better use and security, we must pass "get" as the parameter |
|
28
|
|
|
if(!$this->startsWith(strtolower($get),"get")) |
|
29
|
|
|
{ |
|
30
|
|
|
throw new JsonException("invalid call"); |
|
31
|
|
|
} |
|
32
|
|
|
$email = $this->request->getData("email"); |
|
33
|
|
|
try { |
|
34
|
|
|
$return = !$this->userModel->isEmailUsed($email); |
|
|
|
|
|
|
35
|
|
|
} catch (\Exception $e) { |
|
36
|
|
|
throw new JsonException($e->getMessage()); |
|
37
|
|
|
} |
|
38
|
|
|
echo json_encode($return); |
|
39
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @throws JsonException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function toggleActivation() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->onlyAdmin(); |
|
48
|
|
|
$this->onlyPost(); |
|
49
|
|
|
$state = (bool)($this->request->getData("state") === 'true'); |
|
50
|
|
|
$userId = (int)$this->request->getData("userId"); |
|
51
|
|
|
|
|
52
|
|
|
$result = array(); |
|
53
|
|
|
$result["success"] = false; |
|
54
|
|
|
$result["state"] = $state; |
|
55
|
|
|
$result["userId"] = $userId; |
|
56
|
|
|
|
|
57
|
|
|
// we can not update the Original Admin activation state |
|
58
|
|
|
if($userId !== 1) |
|
59
|
|
|
{ |
|
60
|
|
|
$result["success"] = $this->userModel->activateUser(!$state, $userId); |
|
61
|
|
|
$result["state"] = !$state; |
|
62
|
|
|
$result["userId"] = $userId; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
echo json_encode($result); |
|
66
|
|
|
} |
|
67
|
|
|
} |