Professional Documents
Culture Documents
Bài thực hành phân quyền người dùng trong ứng dụng Web
Bài thực hành phân quyền người dùng trong ứng dụng Web
HỌC PHẦN
An toàn ứng dụng Web
Hà Nội, 2019
MỤC LỤC
1. Điều kiện tiên quyết............................................................................................. 1
2. Giới thiệu .............................................................................................................. 1
3. Kịch bản thực hành ............................................................................................. 1
4. Mục tiêu bài thực hành ....................................................................................... 1
5. Tổ chức thực hành............................................................................................... 1
6. Môi trường thực hành......................................................................................... 2
7. Sơ đồ thực hành ................................................................................................... 2
8. Các nhiệm vụ cần thực hiện ............................................................................... 2
Nhiệm vụ 1. Xây dựng Database ............................................................................ 2
Nhiệm vụ 2. Xây dựng module phân quyền.......................................................... 3
Nhiệm vụ 3. Thực hiện phân quyền người dùng theo vai trò ........................... 22
9. Đánh giá bài thực hành......................................... Error! Bookmark not defined.
DANH MỤC HÌNH ẢNH
ii
1. Điều kiện tiên quyết
Bài thực hành xác thực người dùng trong ứng dụng Web
2. Giới thiệu
Bất kể hệ thống website nào cũng có người dùng và đi kèm với nó là việc
xác thực (authentication) và phân quyền (authorization) với từng người dùng. Sau
khi người dùng đăng nhập thành công, nhiệm vụ tiếp theo của ứng dụng web là xác
định những tài nguyên mà người dùng được phép truy cập. Thao tác này được gọi
là phân quyền người dùng (users authorization). Có 3 mô hình phân quyền truy cập
điển hình là: phân quyền truy cập tùy quyền (discretionary access control), phân
quyền truy cập bắt buộc (mandatory access control) và phân quyền dựa vào vai trò
(role – based access control). Đối với ứng dụng web, mô hình phân quyền thường
được dùng là phân quyền dựa vào vai trò.
Trong bài thực hành này, chúng ta sẽ thực hiện phân quyền người dùng dựa
vào vai trò trong ứng dụng web.
1
6. Môi trường thực hành
- Yêu cầu phần cứng:
+ Số lượng máy tính cần chuẩn bị : 1
+ Cấu hình tối thiểu của Intel Core i3, 4GB RAM
- Yêu cầu phần mềm trên máy:
+ Hệ điều hành trên máy tính : Window
+ Phần mềm : Sublime Text 3, Xampp
- Yêu cầu kết nối mạng Internet: có
7. Sơ đồ thực hành
Không có
Hình 1. Sơ đồ database
Tạo bảng users như sau:
CREATE TABLE users
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
password VARCHAR(100),
email VARCHAR(100),
created_at TIMESTAMP,
updated_at TIMESTAMP
)
ALTER TABLE users
ADD PRIMARY KEY (id);
Tạo bảng user_has_roles:
CREATE TABLE `user_has_roles`
(
id int(10) UNSIGNED NOT NULL,
2
role_id int(10) UNSIGNED NOT NULL,
user_id int(10) UNSIGNED NOT NULL
)
Tạo khóa ngoại cho các bảng user_has_roles:
ALTER TABLE role_has_permissions
ADD PRIMARY KEY (id),
ADD KEY role_has_permissions_permission_id_foreign (permission_id),
ADD KEY role_has_permissions_role_id_foreign (role_id);
Tạo ràng buộc giữa bảng users và bảng user_has_roles:
ALTER TABLE user_has_roles
ADD CONSTRAINT user_has_roles_ibfk_1 FOREIGN KEY (user_id)
REFERENCES users (id)
Tương tự tạo và liên kết các bảng còn lại như sơ đồ trên.
Nhiệm vụ 2. Xây dựng module phân quyền
Bước 1: Tạo thư mục phanquyen trong thư mục htdocs
Tạo thư mục phanquyen/includes để chứa các file thư viện kết nối database
Bước 2: Tạo kết nối đến mysql để lấy dữ liệu:
- Tạo file database.php trong thư mục phanquyen/includes:
<?php
$database['host'] = 'localhost'; //Tên Hosting
$database['dbname'] = 'tech_php'; //Tên của Database
$database['username'] = 'root'; //Tên sử dụng Database
$database['password'] = ''; //Mật khẩu sử dụng Database
$connect=mysqli_connect("{$database['host']}","{$database['userna
me']}","{$database['password']}"); // Tạo kết nối
mysqli_select_db($connect, "{$database['dbname']}") or die("Không
thể chọn database"); // chọn bảng
mysqli_set_charset($connect, 'UTF8'); // set ngôn ngữ
?>
Sau khi tạo xong file kết nối database, khi tạo thẻ <?php ?> ở phần đầu các
file .php cần thêm dòng code sau để kết nối đến database:
include 'includes/database.php';
Bước 3: Tạo liên kết tới các file css,bootstrap
Khi tạo các file .php, các thẻ liên kết này thường được đặt ở phần đầu và để
trong thẻ <head></head>.
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
// tạo title
<title> Danh sách thành viên - Hệ Thống Quản Trị </title>
// sử dụng bootstrap
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
// sử dụng font
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-
awesome/4.7.0/css/font-awesome.min.css">
// sử dụng javasscript
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></scri
pt>
3
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js">
</script>
Bước 4: Tạo file login.php trong thư mục phanquyen để thực hiện đăng
nhập:
- Tạo form login như sau:
4
$password = md5($password); // Mã hóa md5 mật khẩu
$query = mysqli_query($connect, "SELECT
users.email, users.id as user_id, users.name as user_name,
GROUP_CONCAT(permissions.name) as permission_name,
user_has_roles.user_id,
roles.name as role_name,
roles.id as role_id
FROM users
LEFT JOIN user_has_roles ON users.id = user_has_roles.user_id
LEFT JOIN roles ON roles.id = user_has_roles.role_id
LEFT JOIN role_has_permissions ON role_has_permissions.role_id = roles.id
LEFT JOIN permissions ON role_has_permissions.permission_id = permissions.id
WHERE email = '$email' AND password = '$password'
GROUP BY roles.id");
if (mysqli_num_rows($query) > 0) { $permissions = '';
while ($row = mysqli_fetch_array($query)) {
$permissions .= $row['permission_name'];
$_SESSION['auth_user'] = [
'id' => $row['user_id'],
'name' => $row['user_name'],
'email' => $row['email']
];
}
$_SESSION['auth_user']['permission_name'] = explode(',', $permissions);
header('Location: ' . 'index.php'); // return redirect về login
exit();
}
else {
header('Location: ' . $_SERVER['HTTP_REFERER']); // return back
exit;
}
}
Bước 5: Tạo file logout.php trong thư mục phanquyen để thực hiện đăng
xuất:
Sử dụng đoạn code dưới đây để thực hiện đăng xuất: Khi user đăng xuất sẽ
quay trở về trang login.
<?php
session_start();// khởi tạo session
session_destroy();
header('Location: ' . 'login.php');
?>
Bước 6: Tạo file nav.php trong thư mục phanquyen/includes để tạo
menubar:
Tạo menu với thông tin sau: Trang chủ, Quản lý user, Quản lý vai trò, Quản
lý quyền, Đăng xuất.
5
<div class="container">
<ul class="nav navbar-nav">
<li>
<a href="index.php">Trang chủ</a>
</li>
<li class="active">
<a href="users.php">Quản lý User</a>
</li>
<li>
<a href="roles.php">Quản lý vai trò</a>
</li>
<li>
<a href="permission.php">Quản lý quyền</a>
</li>
<li>
<a href="logout.php">Đăng Xuất</a>
</li>
</ul>
</div>
</nav>
Kiểm tra khi user login vào sẽ có vai trò và quyền hạn nào với website, từ đó
sẽ có quyền được truy cập vào các tab menu nào bên trên, thêm dòng code sau để
thực hiện kiểm tra:
<?php if(in_array('Phân quyền',$_SESSION['auth_user']['permission_name'])) {
?> // dòng này ở dưới dòng code tạo tab Trang chủ
<?php } ?> // dòng này ở dưới dòng code tạo tab Quản lý quyền
Sau khi tạo xong, khi tạo các file .php thêm dòng code vào phần
<body><body> để thực hiện kết nối tới file nav.php để hiển thị menu:
<?php include 'includes/nav.php'; ?>
Bước 7: Tạo file permissions.php trong thư mục phanquyen để thực hiện
tạo và hiển thị các quyền trong hệ thống
- Tạo form thêm mới quyền như sau:
6
Hình 4. Form thêm mới quyền
Thực hiện đoạn code sau để tạo form thêm mới:
<h2> Quyền </h2>
<div class="col-md-6">
<h3> Thêm mới </h3>
<div class="panel">
<form method="post">
<div class="form-group">
<label for="name"> Tên: </label>
<input type="text" name="name" required class="form-control">
</div>
<div class="form-group">
<label for="description"> Mô tả: </label>
<textarea name="description" cols="30" rows="10" class="form-
control"></textarea>
</div>
<div class="form-group">
<input type="hidden" name="add-permission" value="1">
<input type="submit" class="btn btn-sm btn-warning"
value="Thêm mới">
</div>
</form>
</div>
</div>
<div class="col-md-6">
<table class="table table-bordered" style="margin-top: 60px;border:1px
solid #eee">
<thead>
<tr>
<th>ID</th>
<th>Tên</th>
<th>Mô tả</th>
</tr>
7
</thead>
<tbody>
<?php foreach ($permissions as $item) { ?>
<tr>
<td><?= $item['id']; ?></td>
<td><?= $item['name']; ?></td>
<td><?= $item['description']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
Tiếp theo, thực hiện câu lệnh SQL trong thẻ <?php ?> ở phần đầu file để
truy vấn insert dữ liệu vào bảng trong database:
if (isset($_POST['add-permission-groups'])) {
$name = $_POST['name'];
$description = $_POST['description'];
$created_at = date("Y-m-d H:i:s");
$sql_insert = "INSERT INTO permission_groups(name, description, created_at)
VALUES ('{$name}','{$description}','{$created_at}')";
$query_insert = mysqli_query($connect, $sql_insert);
if ($query_insert) // nếu lưu thành công
{
header('Location: ' . '?page=dang-nhap'); // trở về login
exit;
}
else // nếu thất bại
{
header('Location: ' . $_SERVER['HTTP_REFERER']);// back
exit;
}
}
- Tạo bảng hiển thị danh sách quyền như sau:
8
</tbody>
</table>
</div>
Tiếp theo, thực hiện câu lệnh SQL trong thẻ <?php ?> ở phần đầu file để
truy vấn lấy dữ liệu từ bảng trong database để hiển thị danh sách quyền:
$sql = "SELECT * FROM permissions";
$query = mysqli_query($connect, $sql);
$num_rows = mysqli_num_rows($query);
$permissions = array();
if ($num_rows > 0) {
while ($row = mysqli_fetch_array($query)) {
$permissions[] = [
'id' => $row['id'],
'name' => $row['name'],
'description' => $row['description']
];
}
}
Sau khi thực hiện truy vấn, thêm đoạn code sau vào thẻ <tbody></tbody> để
hiển thị danh sách các quyền:
<?php foreach ($permission_groups as $item) { ?>
<tr>
<td><?= $item['id']; ?></td>
<td><?= $item['name']; ?></td>
<td><?= $item['description']; ?></td>
</tr>
<?php } ?>
Bước 8: Tạo file add-role.php trong thư mục phanquyen để thực hiện việc
thêm vai trò cho người dùng:
- Tạo form thêm mới vai trò như sau:
9
<div class="col-md-6">
<h2> Chọn quyền cho vai trò </h2>
</div>
</div>
<form method="post">
<div class="col-md-12" style="margin-top:30px;">
<div class="col-md-6" style="max-width:500px;border:1px solid
#eee;padding:20px;">
<div class="form-group">
<label for="name">Tên:</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label for="description">Mô tả:</label>
<textarea class="form-control" name="description" cols="30"
rows="10"></textarea>
</div>
</div>
<div class="col-md-6">
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading"> Chọn quyền: </div>
<div class="panel-body">
// vòng lặp lấy tất cả tất các quyền trong hệ thống
<?php foreach ($permissions as $group) { ?>
<div class="form-group">
<label><input name="permissions[]"
type="checkbox" value="<?= $group['id'] ?>">
<?= $group['name'] ?></label>
</div>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-info" value="Thêm mới">
<a class="btn btn-default" href="roles.php"> Hủy </a>
</div>
</form>
Tiếp theo thực hiện câu lệnh sau trong thẻ <?php ?> ở phần đầu file để lấy
dữ liệu từ bảng permissions, lấy tất cả các quyền có trong hệ thống để chọn quyền
cho vai trò:
$sql_pms = "SELECT * FROM permissions";
$query_pms = mysqli_query($connect, $sql_pms);
$num_rows_pms = mysqli_num_rows($query_pms);
$permissions = array();
if ($num_rows_pms > 0) {
while ($row_pms = mysqli_fetch_array($query_pms)) {
$permission_groups = $row_pms['permission_group'];
$permissions[$permission_groups][] = [
'id' => $row_pms['id'], 'name' => $row_pms['name']
];
}
10
}
Sau khi lấy tất cả các quyền có trong database, sử dụng đoạn code sau để
thực hiện việc insert dữ liệu vào bảng roles khi người dùng click button “Thêm
mới”:
$role_id = false; // tạo biến role_id
$sql = "INSERT INTO roles(name,description,created_at) VALUE
('{$name}','{$description}','{$created_at}')";
$insert = mysqli_query($connect, $sql);
if ($insert == true) {
$role_id = mysqli_insert_id($connect
if (count($in_permissions) > 0 && $role_id != false) {
foreach ($in_permissions as $pms_id) {
mysqli_query($connect,"INSERT INTO
role_has_permissions (permission_id,role_id)
VALUE('{$pms_id}','{$role_id}')");
}
}
header('Location: ' . 'roles.php');
exit;
}
else {
header('Location: ' . $_SERVER['HTTP_REFERER']);// return back
exit;
}
}
Bước 9: Tạo file roles.php trong thư mục phanquyen để hiển thị danh
sách vai trò:
Tạo danh sách vai trò như sau:
11
<th>ID</th>
<th>Tên</th>
<th>Mô tả</th>
<th style="width:200px"></th>
</tr>
</thead>
<tbody>
//sử dụng vòng lặp để lấy tất cả các vai trò trong hệ thống
<?php foreach ($roles as $item) { ?>
<tr>
<td><?= $item['id']; ?></td>
<td><?= $item['name']; ?></td>
<td><?= $item['description']; ?></td>
<td style="width:200px;text-align:center">
<a href="edit-role.php?id=<?= $item['id']; ?>"
class="btn btn-success btn-sm"> Sửa vai trò </a>
<a onclick="return confirm('Bạn có chắc xóa không
?')" href="delete-role.php?id=<?= $item['id']; ?>"
class="btn btn-warning btn-sm"> Xóa vai trò </a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
}
Sau khi tạo form, thực hiện câu lệnh SQL sau trong thẻ <?php ?> ở phần đầu
file để lấy dữ liệu từ database:
$sql = "SELECT * FROM roles WHERE deleted_at IS NULL";
$query = mysqli_query($connect, $sql);
$num_rows = mysqli_num_rows($query);
$roles = array();
if ($num_rows > 0) {
while ($row = mysqli_fetch_array($query)) {
$roles[] = [
'id' => $row['id'],
'name' => $row['name'],
'description' => $row['description']
];
}
}
Bước 10: Tạo file edit-role.php trong thư mục phanquyen để thực hiện
chỉnh sửa các vai trò:
Lưu ý: form chỉnh chửa giống form tạo mới vai trò, có thể copy bên file add-
role.php:
<h2> Chỉnh sửa vai trò </h2>
<form method="post">
<div class="col-md-12" style="margin-top:30px;">
<div class="col-md-6" style="max-width:500px;border:1px solid
#eee;padding:20px;">
<div class="form-group">
<label for="name">Tên:</label>
12
<input type="text" value="<?= $role_data['name']; ?>"
class="form-control" name="name">
</div>
<div class="form-group">
<label for="description">Mô tả:</label>
<textarea class="form-control" name="description"
cols="30" rows="10"><?= $role_data['description']; ?></textarea>
</div>
</div>
<div class="col-md-6">
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading"> Chọn quyền </div>
<div class="panel-body">
<?php foreach ($permissions as $group) { ?>
<div class="form-group">
<label>
<input name="permissions[]" <?php
if(in_array($group['id'], $role_data['permission'])) echo "checked"; ?>
type="checkbox" value="<?= $group['id'] ?>">
<?= $group['name'] ?>
</label>
</div>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-info" value="Cập nhật">
<a class="btn btn-default" href="roles.php"> Hủy </a>
</div>
</form>
Tiếp theo thực hiện câu lệnh truy vấn sau trong thẻ <?php ?> ở phần đầu file
để lấy dữ liệu từ bảng permissions:
// Lấy tất cả quyền đổ ra checkbox
$sql_pms = "SELECT * FROM permissions";
$query_pms = mysqli_query($connect, $sql_pms);
$num_rows_pms = mysqli_num_rows($query_pms);
$permissions = array();
if ($num_rows_pms > 0) {
while ($row_pms = mysqli_fetch_array($query_pms)) {
$permissions[] = [
'id' => $row_pms['id'], 'name' => $row_pms['name']
];
}
}
Sau khi lấy tất cả các quyền, thực hiện câu lệnh truy vấn sau để lấy thông tin
cũ vai trò để chỉnh sửa:
if (isset($_GET['id'])) {
$id = $_GET['id'];
13
$query_role = mysqli_query($connect, "SELECT * FROM
roles LEFT JOIN role_has_permissions ON roles.id =
role_has_permissions.role_id WHERE roles.id = '$id'");
$role_data = array();
while ( $data = mysqli_fetch_array($query_role) ) {
$role_data['name'] = $data['name'];
$role_data['description'] = $data['description'];
$role_data['permission'][] = $data['permission_id'];
}
}
Tiếp theo, thực hiện kiểm tra khi người dùng click button “Cập nhật”:
if (isset($_POST['name'])) {
$name = $_POST['name'];
$description = isset($_POST['description']) ?
$_POST['description'] : null;
$in_permissions = isset($_POST['permissions']) ?
$_POST['permissions'] : [];
$name = trim($name);
$description = trim($description);
if ($update) {
$role_id = $id;
if (count($in_permissions) > 0) {
$remove = mysqli_query($connect, "DELETE FROM
role_has_permissions WHERE role_id = '$role_id'");
foreach ($in_permissions as $pms_id) {
$update_rl_has_pms = mysqli_query($connect, "INSERT INTO
role_has_permissions(permission_id,role_id) VALUE
('{$pms_id}','{$role_id}')");
}
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
}
else {
header('Location: ' . $_SERVER['HTTP_REFERER']); //return back
exit;
}
}
Bước 11: Tạo file delete-role.php để thực hiện xóa vai trò
14
Sử dụng đoạn code sau để thực hiện xóa vai trò khi người dùng click vào
button “Xóa vai trò”:
if (isset($_GET['id']))
{
$id = $_GET['id'];
$query = mysqli_query($connect, $sql);
if ($query) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
}
else {
header('Location: ' . $_SERVER['HTTP_REFERER']); //
return back
exit;
}
}
Bước 12: Tạo file add-user.php để thêm mới user
Tạo form thêm user như sau:
15
<div class="form-group">
<input type="password" id="password" class="form-
control" name="password" placeholder="Mật khẩu">
</div>
<div class="form-group">
<input type="submit" class="btn btn-sm btn-warning"
value="Thêm thành viên">
</div>
</form>
</div>
<style>
.form_add_user {
max-width: 500px;
margin: 0 auto;
border: 1px solid #eee;
border-radius: 3px;
padding: 20px;
margin-top: 50px;
}
</style>
Thực hiện câu lệnh sau trong thẻ <?php ?> ở phần đầu file để kiểm tra khi
người dùng click button “Thêm thành viên” và truy vấn insert dữ liệu:
if (isset($_POST['name']) && isset($_POST['email']) &&
isset($_POST['password'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$password = trim($_POST['password']);
$password = md5($password); // Mã hóa md5 mật khẩu
$sql = "INSERT INTO users(name, email, password, created_at) VALUE
('{$name}','{$email}','{$password}','{$created_at}')"; // Tạo
Query SQL
$insert = mysqli_query($connect, $sql); // Lưu Thông tin đăng ký
vào bảng users
if ($insert) // nếu lưu thành công
{
header('Location: ' . '?page=dang-nhap'); // return redirect về
login
exit;
}
else // nếu thất bại
{
header('Location: ' . $_SERVER['HTTP_REFERER']); // return back
exit;
}
}
Bước 13: Tạo file users.php trong thư mục phanquyen để thực hiện hiển
thị danh sách các user có trong database:
Tạo danh sách hiển thị user như sau:
16
Hình 9. Bảng hiển thị danh sách user
Thực hiện đoạn code sau để tạo danh sách user:
<div class="panel" style="box-shadow: none;">
<h2> Quản lý thành viên </h2>
<a class="btn btn-sm btn-primary" href="add-user.php"> Thêm thành viên
</a>
<table class="table table-bordered" style="margin-top:
60px;border:1px solid #eee; width: 800px">
<thead>
<tr>
<th>ID</th>
<th>Tên</th>
<th>Email</th>
<th>Vai trò</th>
<th style="width:50px">Chọn</th>
</tr>
</thead>
<tbody>
<?php foreach ($list_user as $item) { ?>
<tr class="item__user" data-name="<?=
$item['name']; ?>">
<td><?= $item['id']; ?></td>
<td><?= $item['name']; ?></td>
<td><?= $item['email']; ?></td>
<td><?= $item['role_name']; ?></td>
<td style="text-align:center">
<form action="user-role.php" method="post">
<input type="hidden"
name="role_group" value="<?= $item['role_id']; ?>">
<input type="hidden"
name="user_name" value="<?=
$item['name'].'+'.$item['id'];; ?>">
<button class="btn btn-sm btn-
info"> Chọn vai trò </button>
</form>
</td>
</tr>
17
</tbody>
</table>
</div>
Thực hiện câu lệnh sau trong thẻ <?php ?> ở phần đầu file để lấy dữ liệu từ
trong database:
$sql = "SELECT
GROUP_CONCAT(roles.name) as name,
GROUP_CONCAT(roles.id) as role_id,users.id AS user_id,
email,users.NAME AS user_name
FROM users
LEFT JOIN user_has_roles ON users.id =
user_has_roles.user_id
LEFT JOIN roles ON roles.id = user_has_roles.role_id
GROUP BY users.id";
$query = mysqli_query($connect, $sql);
$num_rows = mysqli_num_rows($query); // đếm số bản ghi
$list_user = array(); // tạo mảng chứa dữ liệu trả về
if ($num_rows > 0) {
while ($row = mysqli_fetch_array($query)) {
$list_user[] = [
'id' => $row['user_id'],
'name' => $row['user_name'],
'email' => $row['email'],
'role_id' => $row['role_id'],
'role_name' => ($row['name']) ? $row['name'] : 'Chưa có vai
trò'
];
}
}
Bước 14: Tạo file user-role.php để thực hiện gán quyền cho user:
Tạo form gán quyền như sau:
19
Sau đó, thực hiện kiểm tra khi người dùng click button “Lưu”:
if (isset($_POST['user_name'])) {
$user_name = explode('+',$_POST['user_name']);
$us_name = $user_name[0];
$us_id = $user_name[1];
}
$data = array();
if ($num_rows > 0) {
while ($row = mysqli_fetch_array($query)) {
20
$data[] = [
'role_name' => $row['role_name'],
'permissions' => explode(',',
$row['permission_name'])
];
}
}
Tạo giao diện kiểm tra:
21
<li><?= $pe; ?></li>
<?php } ?>
</ul>
</div>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
<?php } else { ?>
<p> Bạn không có vai trò nào trong hệ thống </p>
<?php } ?>
</div>
Nhiệm vụ 3. Thực hiện phân quyền người dùng theo vai trò
Bước 1: Tạo danh sách quyền
Tạo danh sách các quyền như dưới đây:
- Thêm mới sản phẩm
- Xem danh sách sản phẩm
- Sửa sản phẩm
- Xóa sản phẩm
- Đăng tin tức
- Xem danh sách tin tức
- Xóa tin tức
- Sửa tin tức
- Xem danh sách user
- Xóa user
- Sửa user
- Thêm user
- Phân quyền theo vai trò
- Xem danh sách đơn hàng
- Xóa đơn hàng
- Tạo đơn hàng
Bước 2: Tạo vai trò
- Tạo vai trò Admin: Admin có tất cả các quyền trong hệ thống
- Tạo vai trò Biên tập viên với tất cả các quyền sau:
+ Đăng tin tức
+ Xem danh sách tin tức
+ Xóa tin tức
+ Sửa tin tức
- Tạo vai trò Nhân viên bán hàng với các quyền sau:
+ Thêm mới sản phẩm
+ Xem danh sách sản phẩm
+ Sửa sản phẩm
22
+Xóa sản phẩm
Bước 3: Tạo danh sách user
- Tạo user1 với vai trò là Admin
- Tạo user2 với vai trò là Biên tập viên
- Tạo user3 với vai trò là Nhân viên bán hàng
- Tạo user4 với vai trò là Biên tập viên, Nhân viên bán hàng
- Tạo user5 không có vai trò nào trong hệ thống
Bước 4: Kiểm tra phân quyền
Đăng nhập tài khoản mỗi user và kiểm tra vai trò với các quyền tương ứng
với vai trò đó
23