| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Services; |
| 4 |
|
| 5 |
class AuthorizationService { |
| 6 |
|
| 7 |
/** |
| 8 |
* Whether the current user has specified capabilities or not. |
| 9 |
* |
| 10 |
* @param string|array $capabilities |
| 11 |
* |
| 12 |
* @return bool |
| 13 |
*/ |
| 14 |
public function currentUserCan( $capabilities ){ |
| 15 |
if( empty( $capabilities ) ){ |
| 16 |
return false; |
| 17 |
} |
| 18 |
|
| 19 |
$capabilities = (array) $capabilities; |
| 20 |
foreach( $capabilities as $capability ){ |
| 21 |
if( current_user_can( $capability ) ){ |
| 22 |
return true; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
return false; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Whether current user is allowed to publish document or not |
| 31 |
* |
| 32 |
* @return bool |
| 33 |
*/ |
| 34 |
public function currentUserCanPublishDocument(){ |
| 35 |
return $this->currentUserCan( [ 'manage_options', 'publish_depicter' ] ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Checks if user has enough quota to publish a new document based on their tier |
| 40 |
* |
| 41 |
* @return bool |
| 42 |
*/ |
| 43 |
public function userHasPublishQuota(){ |
| 44 |
// For free tier users, check if they've reached the limit (currently 2) |
| 45 |
if ( \Depicter::auth()->isFreeTier() ) { |
| 46 |
return \Depicter::documentRepository()->getNumberOfPublishedDocuments() < 2; |
| 47 |
} |
| 48 |
|
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get user session |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public function getUserSession(){ |
| 58 |
if ( !isset( $_COOKIE['depicter_session'] ) ) { |
| 59 |
$session = wp_generate_uuid4(); |
| 60 |
setcookie( 'depicter_session', $session, 86400 * 30, '/' ); |
| 61 |
return $session; |
| 62 |
} |
| 63 |
|
| 64 |
return $_COOKIE['depicter_session']; |
| 65 |
} |
| 66 |
} |
| 67 |
|