FileStorageAccessJoint::getActionUrls()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 4
nop 2
1
<?php 
2
3
namespace Epesi\FileStorage\Integration\Joints;
4
5
use Epesi\Core\System\Modules\ModuleJoint;
0 ignored issues
show
Bug introduced by
The type Epesi\Core\System\Modules\ModuleJoint was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Support\Facades\Auth;
7
8
abstract class FileStorageAccessJoint extends ModuleJoint
9
{
10
	/**
11
	 * Define the route used when generating urls for file access
12
	 * 
13
	 * @var string
14
	 */
15
	protected static $route = 'file';
16
	
17
	/**
18
	 * You can override this variable to define allowed actions
19
	 *
20
	 * @var array Possible actions to execute
21
	 */
22
	protected static $allowedActions = ['download', 'preview', 'inline'];
23
	
24
	/**
25
	 * You can override this variable to allow access for not logged in users
26
	 *
27
	 * @var bool
28
	 */
29
	protected $forUsersOnly = true;
30
	
31
	/**
32
	 * Checks if access for the specified file is granted
33
	 * 
34
	 * @param \Illuminate\Http\Request $request
35
	 * @return boolean
36
	 */
37
	final public function accessGranted($request)
38
	{
39
		$action = $request->get('action', 'download');
40
41
		return $this->hasUserAccess() && $this->hasActionAccess($action) && $this->hasAccess($request);
42
	}
43
	
44
	/**
45
	 * Generates URLs for allowed file actions
46
	 * 
47
	 * @param array $params
48
	 * @return string[]
49
	 */
50
	final public static function getActionUrls($file, $accessParams = [])
51
	{
52
		$id = is_numeric($file)? $file: $file['id'];
53
		
54
		$urls = [];
55
		foreach (static::$allowedActions as $action) {
56
			$urls[$action] = url(static::$route) . '?' . http_build_query(compact('id', 'action') + $accessParams);
57
		}
58
		
59
		return $urls;
60
	}
61
		
62
	/**
63
	 * Determine general user access
64
	 * 
65
	 * @return boolean
66
	 */
67
	final protected function hasUserAccess()
68
	{
69
		return $this->forUsersOnly? (bool) Auth::id(): true;
70
	}
71
	
72
	/**
73
	 * Determine if there is access to the requested action
74
	 * 
75
	 * @param string $action
76
	 * @return boolean
77
	 */
78
	final protected function hasActionAccess($action)
79
	{
80
		return in_array($action, (array) static::$allowedActions);
81
	}
82
	
83
	/**
84
	 * Define custom file access
85
	 * 
86
	 * @param \Illuminate\Http\Request $request
87
	 * @return boolean
88
	 */
89
	protected function hasAccess($request)
90
	{
91
		return Auth::user()->can('download files');
92
	}
93
}