| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Permission; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Helpers\Helper; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
use FluentCart\App\Models\User; |
| 9 |
|
| 10 |
class PermissionManager |
| 11 |
{ |
| 12 |
|
| 13 |
static $metaKey = '_fluent_cart_admin_role'; |
| 14 |
public const ADMIN_CAP = 'fluent_cart_admin'; |
| 15 |
|
| 16 |
public static function getAllRoles(): array |
| 17 |
{ |
| 18 |
|
| 19 |
$allRoles = [ |
| 20 |
'super_admin' => [ |
| 21 |
'title' => __('Super Admin', 'fluent-cart'), |
| 22 |
'descriptions' => __('With All Permissions', 'fluent-cart'), |
| 23 |
'permissions' => array_keys(self::getAllPermissions()) |
| 24 |
], |
| 25 |
'manager' => [ |
| 26 |
'title' => __('Manager', 'fluent-cart'), |
| 27 |
'descriptions' => __('With All Permissions Except Sensitive Settings', 'fluent-cart'), |
| 28 |
'permissions' => [ |
| 29 |
'store/settings', |
| 30 |
'products/view', |
| 31 |
'products/create', |
| 32 |
'products/edit', |
| 33 |
'products/delete', |
| 34 |
'customers/view', |
| 35 |
'customers/manage', |
| 36 |
'customers/export', |
| 37 |
'customers/delete', |
| 38 |
'orders/view', |
| 39 |
'orders/manage_statuses', |
| 40 |
'orders/can_refund', |
| 41 |
'orders/manage', |
| 42 |
'orders/export', |
| 43 |
'orders/delete', |
| 44 |
'subscriptions/view', |
| 45 |
'subscriptions/manage', |
| 46 |
'subscriptions/export', |
| 47 |
'subscriptions/delete', |
| 48 |
'licenses/view', |
| 49 |
'licenses/manage', |
| 50 |
'licenses/export', |
| 51 |
'licenses/delete', |
| 52 |
'coupons/view', |
| 53 |
'coupons/manage', |
| 54 |
'coupons/delete', |
| 55 |
'reports/view', |
| 56 |
'reports/export', |
| 57 |
'integrations/view', |
| 58 |
'integrations/manage', |
| 59 |
'integrations/delete' |
| 60 |
] |
| 61 |
], |
| 62 |
'worker' => [ |
| 63 |
'title' => __('Worker', 'fluent-cart'), |
| 64 |
'descriptions' => __('View Access for products, customers, coupons, integretions. Manage Access for Order Statuses', 'fluent-cart'), |
| 65 |
'permissions' => [ |
| 66 |
'products/view', |
| 67 |
'customers/view', |
| 68 |
'orders/view', |
| 69 |
'orders/manage_statuses', |
| 70 |
'subscriptions/view', |
| 71 |
'licenses/view', |
| 72 |
'coupons/view', |
| 73 |
'coupons/manage', |
| 74 |
'integrations/view' |
| 75 |
] |
| 76 |
], |
| 77 |
'accountant' => [ |
| 78 |
'title' => __('Accountant', 'fluent-cart'), |
| 79 |
'descriptions' => __('View Access for products, customers, orders, subscriptions, licenses, coupons, reports and integrations', 'fluent-cart'), |
| 80 |
'permissions' => [ |
| 81 |
'orders/view', |
| 82 |
'orders/export', |
| 83 |
'reports/view', |
| 84 |
'reports/export', |
| 85 |
'products/view', |
| 86 |
'customers/view', |
| 87 |
'customers/export', |
| 88 |
'subscriptions/view', |
| 89 |
'subscriptions/export', |
| 90 |
'licenses/view', |
| 91 |
'licenses/export', |
| 92 |
'coupons/view', |
| 93 |
'integrations/view' |
| 94 |
] |
| 95 |
] |
| 96 |
]; |
| 97 |
|
| 98 |
return apply_filters('fluent_cart/permission/all_roles', $allRoles, []); |
| 99 |
} |
| 100 |
|
| 101 |
public static function getUserPermissions($userId = false) |
| 102 |
{ |
| 103 |
if ($userId === false) { |
| 104 |
$userId = get_current_user_id(); |
| 105 |
} |
| 106 |
|
| 107 |
$user = get_user_by('ID', $userId); |
| 108 |
|
| 109 |
if (user_can($user, 'manage_options')) { |
| 110 |
$allPermissions = array_keys(self::getAllPermissions()); |
| 111 |
$allPermissions[] = 'is_super_admin'; |
| 112 |
|
| 113 |
return $allPermissions; |
| 114 |
} |
| 115 |
|
| 116 |
if (!App::isProActive()) { |
| 117 |
return []; |
| 118 |
} |
| 119 |
|
| 120 |
$currentRole = get_user_meta($userId, static::$metaKey, true); |
| 121 |
|
| 122 |
|
| 123 |
if (empty($currentRole)) { |
| 124 |
return []; |
| 125 |
} |
| 126 |
|
| 127 |
$allRoles = self::getAllRoles(); |
| 128 |
|
| 129 |
$permissions = Arr::get($allRoles, $currentRole . '.permissions', []); |
| 130 |
|
| 131 |
return $permissions; |
| 132 |
} |
| 133 |
|
| 134 |
public static function hasPermission($permissions, $userId = false): bool |
| 135 |
{ |
| 136 |
if ($userId === false) { |
| 137 |
$userId = get_current_user_id(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
if (!$userId) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
$userPermissions = self::getUserPermissions($userId); |
| 146 |
$permissions = (array)$permissions; |
| 147 |
|
| 148 |
return array_intersect($userPermissions, $permissions) || in_array('super_admin', $userPermissions); |
| 149 |
} |
| 150 |
|
| 151 |
public static function hasAnyPermission(array $permissions, $userId = false): bool |
| 152 |
{ |
| 153 |
if ($userId === false) { |
| 154 |
$userId = get_current_user_id(); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
if (!$userId) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
$userPermissions = self::getUserPermissions($userId); |
| 163 |
|
| 164 |
return !empty(array_intersect($userPermissions, $permissions)) || in_array('is_super_admin', $userPermissions); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
public static function getShopRole($userId) |
| 169 |
{ |
| 170 |
$user = get_user_by('ID', $userId); |
| 171 |
|
| 172 |
if (user_can($user, 'manage_options')) { |
| 173 |
return 'super_admin'; |
| 174 |
} |
| 175 |
|
| 176 |
$currentRole = get_user_meta($userId, static::$metaKey, true); |
| 177 |
|
| 178 |
if (empty($currentRole)) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
return $currentRole; |
| 183 |
} |
| 184 |
|
| 185 |
public static function getAllPermissions(): array |
| 186 |
{ |
| 187 |
return [ |
| 188 |
'store/settings' => __('Store Settings', 'fluent-cart'), |
| 189 |
'store/sensitive' => __('Sensitive Settings', 'fluent-cart'), |
| 190 |
'products/view' => __('View Products', 'fluent-cart'), |
| 191 |
'products/create' => __('Create Products', 'fluent-cart'), |
| 192 |
'products/edit' => __('Edit Products', 'fluent-cart'), |
| 193 |
'products/delete' => __('Delete Products', 'fluent-cart'), |
| 194 |
'customers/view' => __('View Customers', 'fluent-cart'), |
| 195 |
'customers/manage' => __('Manage Customers', 'fluent-cart'), |
| 196 |
'customers/export' => __('Export Customers', 'fluent-cart'), |
| 197 |
'customers/delete' => __('Delete Customers', 'fluent-cart'), |
| 198 |
'orders/view' => __('View Orders', 'fluent-cart'), |
| 199 |
'orders/create' => __('Create Orders', 'fluent-cart'), |
| 200 |
'orders/manage_statuses' => __('Manage Order Statuses', 'fluent-cart'), |
| 201 |
'orders/manage' => __('Manage Orders', 'fluent-cart'), |
| 202 |
'orders/can_refund' => __('Can Refund Orders', 'fluent-cart'), |
| 203 |
'orders/export' => __('Export Orders', 'fluent-cart'), |
| 204 |
'orders/delete' => __('Delete Orders', 'fluent-cart'), |
| 205 |
'subscriptions/view' => __('View Subscriptions', 'fluent-cart'), |
| 206 |
'subscriptions/manage' => __('Manage Subscriptions', 'fluent-cart'), |
| 207 |
'subscriptions/export' => __('Export Subscriptions', 'fluent-cart'), |
| 208 |
'subscriptions/delete' => __('Delete Subscriptions', 'fluent-cart'), |
| 209 |
'licenses/view' => __('View Licenses', 'fluent-cart'), |
| 210 |
'licenses/manage' => __('Manage Licenses', 'fluent-cart'), |
| 211 |
'licenses/export' => __('Export Licenses', 'fluent-cart'), |
| 212 |
'licenses/delete' => __('Delete Licenses', 'fluent-cart'), |
| 213 |
'coupons/view' => __('View Coupons', 'fluent-cart'), |
| 214 |
'coupons/manage' => __('Manage Coupons', 'fluent-cart'), |
| 215 |
'coupons/delete' => __('Delete Coupons', 'fluent-cart'), |
| 216 |
'reports/view' => __('View Reports', 'fluent-cart'), |
| 217 |
'reports/export' => __('Export Reports', 'fluent-cart'), |
| 218 |
'integrations/view' => __('View Integrations', 'fluent-cart'), |
| 219 |
'integrations/manage' => __('Manage Integrations', 'fluent-cart'), |
| 220 |
'integrations/delete' => __('Delete Integrations', 'fluent-cart'), |
| 221 |
'labels/view' => __('View Labels', 'fluent-cart'), |
| 222 |
'labels/manage' => __('Manage Labels', 'fluent-cart'), |
| 223 |
'labels/delete' => __('Delete Labels', 'fluent-cart'), |
| 224 |
'dashboard_stats/view' => __('View Dashboard Stats', 'fluent-cart') |
| 225 |
]; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Get all users that have a FluentCart admin role assigned |
| 230 |
* |
| 231 |
* @return array |
| 232 |
*/ |
| 233 |
public static function getUsersWithShopRole(): array |
| 234 |
{ |
| 235 |
$users = User::query() |
| 236 |
->select(['users.*', 'usermeta.meta_value as shop_role']) |
| 237 |
->join('usermeta', function ($join) { |
| 238 |
$join->on('users.ID', '=', 'usermeta.user_id') |
| 239 |
->where('usermeta.meta_key', '=', static::$metaKey) |
| 240 |
->whereNotNull('usermeta.meta_value')//->where('usermeta.meta_value', '!=', '') |
| 241 |
; |
| 242 |
}) |
| 243 |
->get(); |
| 244 |
|
| 245 |
// get all roles |
| 246 |
$allRoles = self::getAllRoles(); |
| 247 |
|
| 248 |
if ($users->isEmpty()) { |
| 249 |
return []; |
| 250 |
} |
| 251 |
|
| 252 |
return $users->map(function ($user) use ($allRoles) { |
| 253 |
// Check if the shop_role exists in allRoles |
| 254 |
$role = $allRoles[$user->shop_role] ?? []; |
| 255 |
|
| 256 |
return [ |
| 257 |
'id' => (int)$user->ID, |
| 258 |
'email' => $user->user_email, |
| 259 |
'display_name' => $user->display_name, |
| 260 |
'username' => $user->user_login, |
| 261 |
'shop_role' => $user->shop_role, |
| 262 |
'description' => $role['descriptions'] ?? '', |
| 263 |
'registered_at' => $user->user_registered, |
| 264 |
'role_permissions' => self::getUserPermissions($user->ID) |
| 265 |
]; |
| 266 |
})->toArray(); |
| 267 |
} |
| 268 |
|
| 269 |
public static function attachRole($userId, $role) |
| 270 |
{ |
| 271 |
$wpUser = get_user_by('ID', $userId); |
| 272 |
|
| 273 |
if (!$wpUser) { |
| 274 |
return new \WP_Error('user_not_found', __('User not found', 'fluent-cart')); |
| 275 |
} |
| 276 |
if (user_can($wpUser, 'manage_options')) { |
| 277 |
return new \WP_Error('super_admin', __('The user already has all the accesses as part of Administrator Role', 'fluent-cart')); |
| 278 |
} |
| 279 |
// Assign the capability directly to the user |
| 280 |
if (!$wpUser->has_cap(static::ADMIN_CAP)) { |
| 281 |
$wpUser->add_cap(static::ADMIN_CAP); |
| 282 |
} |
| 283 |
|
| 284 |
return update_user_meta($userId, static::$metaKey, $role); |
| 285 |
} |
| 286 |
|
| 287 |
public static function detachRole($userId, $role) |
| 288 |
{ |
| 289 |
$wpUser = get_user_by('ID', $userId); |
| 290 |
|
| 291 |
if (!$wpUser) { |
| 292 |
return new \WP_Error('user_not_found', __('User not found', 'fluent-cart')); |
| 293 |
} |
| 294 |
if (user_can($wpUser, 'manage_options')) { |
| 295 |
return new \WP_Error('super_admin', __('The user already has all the accesses as part of Administrator Role', 'fluent-cart')); |
| 296 |
} |
| 297 |
|
| 298 |
if (!$wpUser->has_cap(static::ADMIN_CAP)) { |
| 299 |
$wpUser->remove_cap(static::ADMIN_CAP); |
| 300 |
} |
| 301 |
|
| 302 |
return delete_user_meta($userId, static::$metaKey); |
| 303 |
} |
| 304 |
|
| 305 |
public static function userCan($permission): bool |
| 306 |
{ |
| 307 |
$currentUser = Helper::getCurrentUser(); |
| 308 |
return $currentUser && $currentUser->userCan($permission); |
| 309 |
} |
| 310 |
} |
| 311 |
|