Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F120833938
ResourceName.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
ResourceName.php
View Options
<?php
namespace
App\Rules
;
use
Illuminate\Contracts\Validation\Rule
;
use
Illuminate\Support\Facades\Validator
;
use
Illuminate\Support\Str
;
class
ResourceName
implements
Rule
{
private
$message
;
private
$owner
;
private
$domain
;
private
const
FORBIDDEN_CHARS
=
'+/^%*!`@(){}|
\\
?<;"'
;
/**
* Class constructor.
*
* @param \App\User $owner The account owner
* @param string $domain The domain name of the group
*/
public
function
__construct
(
$owner
,
$domain
)
{
$this
->
owner
=
$owner
;
$this
->
domain
=
Str
::
lower
(
$domain
);
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute Attribute name
* @param mixed $name Resource name input
*
* @return bool
*/
public
function
passes
(
$attribute
,
$name
):
bool
{
if
(
empty
(
$name
)
||
!
is_string
(
$name
))
{
$this
->
message
=
\trans
(
'validation.nameinvalid'
);
return
false
;
}
if
(
strcspn
(
$name
,
self
::
FORBIDDEN_CHARS
)
<
strlen
(
$name
))
{
$this
->
message
=
\trans
(
'validation.nameinvalid'
);
return
false
;
}
// Check the max length, according to the database column length
if
(
strlen
(
$name
)
>
191
)
{
$this
->
message
=
\trans
(
'validation.max.string'
,
[
'max'
=>
191
]);
return
false
;
}
// Check if specified domain belongs to the user
if
(!
$this
->
owner
->
domains
(
true
,
false
)->
where
(
'namespace'
,
$this
->
domain
)->
exists
())
{
$this
->
message
=
\trans
(
'validation.domaininvalid'
);
return
false
;
}
// Check if the name is unique in the domain
// FIXME: Maybe just using the whole resources table would be faster than resources()?
$exists
=
$this
->
owner
->
resources
()
->
where
(
'name'
,
$name
)
->
where
(
'email'
,
'like'
,
'%@'
.
$this
->
domain
)
->
exists
();
if
(
$exists
)
{
$this
->
message
=
\trans
(
'validation.nameexists'
);
return
false
;
}
return
true
;
}
/**
* Get the validation error message.
*
* @return string
*/
public
function
message
():
?
string
{
return
$this
->
message
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Fri, Apr 24, 12:56 PM (1 w, 2 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
f0/3f/ea51350c72153bab75074a5ea8f7
Default Alt Text
ResourceName.php (2 KB)
Attached To
Mode
rK kolab
Attached
Detach File
Event Timeline