Passed
Push — master ( 0ed784...b28e07 )
by CodexShaper
14:16
created

Manager::checkPermission()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 20
rs 9.6111
1
<?php
2
namespace CodexShaper\DBM;
3
4
use CodexShaper\DBM\Facades\Driver;
5
use CodexShaper\DBM\Models\DBM_Field;
6
use CodexShaper\DBM\Models\DBM_MongoField;
7
use CodexShaper\DBM\Models\DBM_MongoObject;
8
use CodexShaper\DBM\Models\DBM_MongoPermission;
9
use CodexShaper\DBM\Models\DBM_MongoTemplate;
10
use CodexShaper\DBM\Models\DBM_Object;
11
use CodexShaper\DBM\Models\DBM_Permission;
12
use CodexShaper\DBM\Models\DBM_Template;
13
use Illuminate\Filesystem\Filesystem;
14
use Illuminate\Support\Facades\Artisan;
15
use Illuminate\Support\Facades\Auth;
16
use Illuminate\Support\Facades\File;
17
use Illuminate\Support\Facades\Response;
18
use Illuminate\Support\Facades\Route;
19
use Illuminate\Support\Facades\Storage;
20
use Illuminate\Support\Str;
21
22
class Manager
23
{
24
    public function webRoutes()
25
    {
26
        require __DIR__ . '/../routes/web.php';
27
    }
28
29
    public function apiRoutes()
30
    {
31
        require __DIR__ . '/../routes/api.php';
32
    }
33
34
    /**
35
     * Load assests
36
     *
37
     * @param  string $path
38
     *
39
     * @return \Illuminate\Http\Response
40
     */
41
    public function assets($path)
42
    {
43
        $file = base_path(trim(config('dbm.resources_path'), '/') . "/" . urldecode($path));
0 ignored issues
show
Bug introduced by
The function base_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $file = /** @scrutinizer ignore-call */ base_path(trim(config('dbm.resources_path'), '/') . "/" . urldecode($path));
Loading history...
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $file = base_path(trim(/** @scrutinizer ignore-call */ config('dbm.resources_path'), '/') . "/" . urldecode($path));
Loading history...
44
45
        if (File::exists($file)) {
46
47
            switch ($extension = pathinfo($file, PATHINFO_EXTENSION)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $extension is dead and can be removed.
Loading history...
48
                case 'js':
49
                    $mimeType = 'text/javascript';
50
                    break;
51
                case 'css':
52
                    $mimeType = 'text/css';
53
                    break;
54
                default:
55
                    $mimeType = File::mimeType($file);
56
                    break;
57
            }
58
59
            $response = Response::make(File::get($file), 200);
60
            $response->header('Content-Type', $mimeType);
0 ignored issues
show
Bug introduced by
It seems like $mimeType can also be of type false; however, parameter $values of Illuminate\Http\Response::header() does only seem to accept array|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            $response->header('Content-Type', /** @scrutinizer ignore-type */ $mimeType);
Loading history...
61
            $response->setSharedMaxAge(31536000);
62
            $response->setMaxAge(31536000);
63
            $response->setExpires(new \DateTime('+1 year'));
64
65
            return $response;
66
        }
67
68
        return response('', 404);
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        return /** @scrutinizer ignore-call */ response('', 404);
Loading history...
69
    }
70
71
    public function getModelNamespace()
72
    {
73
        return trim(config('dbm.modal_namespace', app()->getNamespace()), '\\');
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        return trim(config('dbm.modal_namespace', /** @scrutinizer ignore-call */ app()->getNamespace()), '\\');
Loading history...
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        return trim(/** @scrutinizer ignore-call */ config('dbm.modal_namespace', app()->getNamespace()), '\\');
Loading history...
74
    }
75
76
    public function makeModel($model, $table)
77
    {
78
        try {
79
80
            $partials = explode("\\", $model);
81
82
            $className = array_pop($partials);
83
            $namespace = implode("\\", $partials);
84
85
            $contents = "<?php\n\n";
86
            $contents .= "namespace " . $namespace . ";\n\n";
87
            if (Driver::isMongoDB()) {
0 ignored issues
show
Bug introduced by
The method isMongoDB() does not exist on CodexShaper\DBM\Facades\Driver. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
            if (Driver::/** @scrutinizer ignore-call */ isMongoDB()) {
Loading history...
88
                $contents .= "use Jenssegers\Mongodb\Eloquent\Model;\n\n";
89
            } else {
90
                $contents .= "use Illuminate\Database\Eloquent\Model;\n\n";
91
            }
92
            $contents .= "class " . $className . " extends Model\n";
93
            $contents .= "{\n\n";
94
            if (Driver::isMongoDB()) {
95
                $contents .= "\tprotected \$collection = '" . $table . "';\n";
96
            } else {
97
                $contents .= "\tprotected \$table = '" . $table . "';\n";
98
            }
99
100
            // $content .= "\tpublic \$timestamps = false;\n";
101
            $contents .= "}\n";
102
103
            $filesystem = new Filesystem;
104
            $filesystem->put(base_path($model . ".php"), $contents);
0 ignored issues
show
Bug introduced by
The function base_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
            $filesystem->put(/** @scrutinizer ignore-call */ base_path($model . ".php"), $contents);
Loading history...
105
        } catch (\Exception $e) {
106
            throw new \Exception("There has an error when create model. The error is :" . $e->getMessage(), 1);
107
108
        }
109
110
    }
111
112
    public function makeController($controller)
113
    {
114
        try {
115
            Artisan::call('make:controller', [
116
                'name' => $controller,
117
            ]);
118
        } catch (\Exception $e) {
119
            throw new \Exception("There has an error when create Controller. The error is :" . $e->getMessage(), 1);
120
121
        }
122
123
    }
124
125
    public function model($model, $table = null)
126
    {
127
        if ($table == null) {
128
            return new $model;
129
        }
130
131
        return (new $model)->setTable($table);
132
    }
133
134
    public function Object()
135
    {
136
        if (Driver::isMongoDB()) {
137
            return new DBM_MongoObject();
138
        }
139
140
        return new DBM_Object;
141
    }
142
143
    public function Field()
144
    {
145
        if (Driver::isMongoDB()) {
146
            return new DBM_MongoField;
147
        }
148
149
        return new DBM_Field;
150
    }
151
152
    public function Permission()
153
    {
154
        if (Driver::isMongoDB()) {
155
            return new DBM_MongoPermission;
156
        }
157
158
        return new DBM_Permission;
159
    }
160
161
    public function Template()
162
    {
163
        if (Driver::isMongoDB()) {
164
            return new DBM_MongoTemplate;
165
        }
166
167
        return new DBM_Template;
168
    }
169
170
    public function templates()
171
    {
172
        $templates    = static::Template()->get();
0 ignored issues
show
Bug Best Practice introduced by
The method CodexShaper\DBM\Manager::Template() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
        $templates    = static::/** @scrutinizer ignore-call */ Template()->get();
Loading history...
173
        $newTemplates = [];
174
175
        foreach ($templates as $template) {
176
            $newTemplates[] = (object) [
177
                "name"          => $template->name,
178
                "oldName"       => $template->old_name,
179
                "type"          => [
180
                    "name" => $template->type,
181
                ],
182
                "notnull"       => $template->notnull,
183
                "unsigned"      => $template->unsigned,
184
                "autoincrement" => $template->auto_increment,
185
                "default"       => $template->default,
186
                "length"        => $template->length,
187
                "index"         => ($template->index != null) ? $template->index : "",
188
            ];
189
        }
190
191
        return $newTemplates;
192
    }
193
194
    /*
195
     * File System
196
     */
197
198
    public function getPathPrefix($driver = 'local')
199
    {
200
        return trim(Storage::disk($driver)->getDriver()->getAdapter()->getPathPrefix(), DIRECTORY_SEPARATOR);
201
    }
202
203
    /*
204
     * Permission
205
     */
206
207
    public function userPermissions()
208
    {
209
        $user = Auth::user();
210
211
        return self::Object()
0 ignored issues
show
Bug Best Practice introduced by
The method CodexShaper\DBM\Manager::Object() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
        return self::/** @scrutinizer ignore-call */ Object()
Loading history...
212
            ->setManyToManyRelation(
213
                $user,
214
                static::Permission(),
0 ignored issues
show
Bug Best Practice introduced by
The method CodexShaper\DBM\Manager::Permission() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

214
                static::/** @scrutinizer ignore-call */ 
215
                        Permission(),
Loading history...
215
                'dbm_user_permissions',
216
                'user_id',
217
                'dbm_permission_id'
218
            )
219
            ->belongs_to_many;
0 ignored issues
show
Bug introduced by
The property belongs_to_many does not seem to exist on CodexShaper\DBM\Models\DBM_Object. 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...
220
    }
221
222
    public function isLoggedIn()
223
    {
224
        if (Auth::guest()) {
225
            return Route::has('login') ? redirect(route('login')) : abort(404);
0 ignored issues
show
Bug introduced by
The function abort was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
            return Route::has('login') ? redirect(route('login')) : /** @scrutinizer ignore-call */ abort(404);
Loading history...
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
            return Route::has('login') ? /** @scrutinizer ignore-call */ redirect(route('login')) : abort(404);
Loading history...
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
            return Route::has('login') ? redirect(/** @scrutinizer ignore-call */ route('login')) : abort(404);
Loading history...
226
        }
227
228
        return true;
229
    }
230
231
    public function checkPermission($prefix, $slug)
232
    {
233
        if (Auth::guest()) {
234
            return 'not_logged_in';
235
        }
236
237
        $user_model        = config('dbm.user.model');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

237
        $user_model        = /** @scrutinizer ignore-call */ config('dbm.user.model');
Loading history...
Unused Code introduced by
The assignment to $user_model is dead and can be removed.
Loading history...
238
        $user_table        = config('dbm.user.table');
0 ignored issues
show
Unused Code introduced by
The assignment to $user_table is dead and can be removed.
Loading history...
239
        $user_local_key    = config('dbm.user.local_key');
0 ignored issues
show
Unused Code introduced by
The assignment to $user_local_key is dead and can be removed.
Loading history...
240
        $user_display_name = config('dbm.user.display_name');
0 ignored issues
show
Unused Code introduced by
The assignment to $user_display_name is dead and can be removed.
Loading history...
241
242
        $permissions = $this->userPermissions();
243
244
        foreach ($permissions as $permission) {
245
            if ($permission->prefix == $prefix && $permission->slug == $slug) {
246
                return 'authorized';
247
            }
248
        }
249
250
        return 'not_authorized';
251
252
    }
253
254
    public function authorize($permission)
255
    {
256
        $permission = explode('.', $permission);
257
258
        $prefix = $permission[0];
259
        $slug   = $permission[1];
260
261
        switch ($this->checkPermission($prefix, $slug)) {
262
            case 'not_logged_in':
263
                return response()->json([
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

263
                return /** @scrutinizer ignore-call */ response()->json([
Loading history...
264
                    'success' => false,
265
                    'url'     => route('login'),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

265
                    'url'     => /** @scrutinizer ignore-call */ route('login'),
Loading history...
266
                ]);
267
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
268
269
            case 'not_authorized':
270
                return response()->json([
271
                    'success' => false,
272
                    'errors'  => ["You don't have permission to " . $slug . " " . $prefix],
273
                ], 401);
274
                break;
275
            case 'authorized':
276
                return true;
277
                break;
278
        }
279
    }
280
281
}
282