Untitled
unknown
plain_text
2 years ago
1.7 kB
6
Indexable
// write your <?php
class User {
private $name;
private $role;
public function __construct($name, $role) {
$this->name = $name;
$this->role = $role;
}
// Getter for the user's role
public function getRole() {
return $this->role;
}
// Getter for the user's name
public function getName() {
return $this->name;
}
}
class SecuritySystem {
private $permissions;
public function __construct() {
$this->permissions = [];
}
// Function to assign permissions to roles
public function assignPermissions($role, $permissions) {
$this->permissions[$role] = $permissions;
}
// Function to check if a user has permission for a specific action
public function hasPermission($user, $action) {
$role = $user->getRole();
if (isset($this->permissions[$role]) && in_array($action, $this->permissions[$role])) {
return true;
}
return false;
}
}
// Example usage
$securitySystem = new SecuritySystem();
// Assign permissions to roles
$securitySystem->assignPermissions('SuperAdmin', ['manageUsers', 'configureSystem', 'accessDashboard']);
$securitySystem->assignPermissions('Admin', ['manageUsers', 'accessDashboard']);
$securitySystem->assignPermissions('User', ['accessDashboard']);
// Create a user with the SuperAdmin role
$user = new User('Jack Alexander Zimmer', 'SuperAdmin');
// Check if the user has permission to access the dashboard
if ($securitySystem->hasPermission($user, 'accessDashboard')) {
echo 'Welcome to your personalized dashboard, ' . $user->getName() . '!';
// Display the personalized dashboard here
} else {
echo 'Access denied!';
}
?>Editor is loading...