Passed
Push — master ( 805c58...4a8ad5 )
by Nikita
10:14 queued 04:50
created

UsersController::saveServerPermission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Gameap\Http\Controllers\API;
4
5
use Gameap\Http\Requests\API\Admin\SaveUserServerPermissionsRequest;
6
use Gameap\Models\Server;
7
use Gameap\Models\User;
8
use Gameap\Repositories\UserRepository;
9
use Illuminate\Contracts\Auth\Factory as AuthFactory;
10
use Illuminate\Http\Request;
11
use Gameap\Http\Controllers\AuthController;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Gameap\Http\Controllers\API\AuthController. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use Gameap\Helpers\ServerPermissionHelper;
13
14
class UsersController extends AuthController
15
{
16
    /**
17
     * The UserRepository instance.
18
     *
19
     * @var \Gameap\Repositories\UserRepository
20
     */
21
    protected $repository;
22
23
    /** @var AuthFactory */
24
    protected $authFactory;
25
26
    /**
27
     * @param  \Gameap\Repositories\UserRepository $repository
28
     */
29
    public function __construct(
30
        UserRepository $repository,
31
        AuthFactory $authFactory
32
    ) {
33
        parent::__construct();
34
35
        $this->repository = $repository;
36
        $this->authFactory = $authFactory;
37
    }
38
39
    public function index()
40
    {
41
        $users = User::all();
42
43
        return response()->json($users);
44
    }
45
46
    public function store(Request $request)
47
    {
48
        $user = $this->repository->store($request->all());
49
50
        return response()->json($user, 201);
51
    }
52
53
    public function show($id)
54
    {
55
        $user = User::findOrFail($id);
56
57
        return response()->json(
58
            [
59
                'id' => $user->id,
60
                'login' => $user->login,
61
                'email' => $user->email,
62
                'name' => $user->name,
63
                'created_at' => $user->created_at,
64
                'updated_at' => $user->updated_at,
65
                'roles' => $user->roles->pluck('name')
0 ignored issues
show
Bug introduced by
The property roles does not seem to exist on Gameap\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
66
            ]
67
        );
68
    }
69
70
    public function update(Request $request, $id)
71
    {
72
        $user = User::findOrFail($id);
73
74
        $this->repository->update($user, $request->all());
75
76
        return response()->json($user, 200);
77
    }
78
79
    public function destroy($id)
80
    {
81
        /** @var User $currentUser */
82
        $currentUser = $this->authFactory->guard()->user();
83
84
        if ($currentUser == $id) {
85
            return response()->json(['message' => __('users.delete_self_error_msg')], 422);
86
        }
87
88
        User::destroy($id);
89
90
        return response()->json(null, 204);
91
    }
92
93
    public function servers($id)
94
    {
95
        /** @var User $user */
96
        $user = User::findOrFail($id);
97
98
        return response()->json($user->servers->map(function (Server $server) {
99
            return [
100
                'id' => $server->id,
101
                'uuid' => $server->uuid,
102
                'uuid_short' => $server->uuid_short,
103
                'enabled' => $server->enabled,
104
                'installed' => $server->installed,
105
                'blocked' => $server->blocked,
106
                'name' => $server->name,
107
                'game_id' => $server->game_id,
108
                'game_mod_id' => $server->game_mod_id,
109
                'game' => [
110
                    'code' => $server->game->code,
111
                    'name' => $server->game->name,
112
                    'engine' => $server->game->engine,
113
                    'engine_version' => $server->game->engine_version,
114
                ],
115
                'game_mod' => [
116
                    'id' => $server->gameMod->id,
117
                    'name' => $server->gameMod->name,
118
                ],
119
                'ds_id' => $server->ds_id,
120
                'expires' => $server->expires,
121
                'server_ip' => $server->server_ip,
122
                'server_port' => $server->server_port,
123
                'query_port' => $server->query_port,
124
                'rcon_port' => $server->rcon_port,
125
            ];
126
        }));
127
    }
128
129
    public function serverPermissions($id, Server $server)
130
    {
131
        /** @var User $user */
132
        $user = User::findOrFail($id);
133
134
        $allPermissions = ServerPermissionHelper::getAllPermissions();
135
136
        $permissions = [];
137
138
        foreach ($allPermissions as $permission) {
139
            $permissions[] = [
140
                'permission' => $permission,
141
                'value'      => $user->can($permission, $server),
142
                'name'       => __('users.permission_names.' . $permission),
143
            ];
144
        }
145
146
        return response()->json($permissions);
147
    }
148
149
    public function saveServerPermission(SaveUserServerPermissionsRequest $request, $id, Server $server)
150
    {
151
        /** @var User $user */
152
        $user = User::findOrFail($id);
153
154
        $permissions = $request->all();
155
156
        $this->repository->saveServerPermission($user, $server, $permissions);
157
158
        return response()->json(['message' => 'success'], 204);
159
    }
160
}
161