Skip to content

Commit d816a0a

Browse files
Project Delivered
1 parent c7679e8 commit d816a0a

File tree

1 file changed

+357
-3
lines changed

1 file changed

+357
-3
lines changed

Mini-Project/7/README.md

+357-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,364 @@
11
# 20CYS383 Java Programming Lab
22
![](https://img.shields.io/badge/Batch-21CYS-lightgreen) ![](https://img.shields.io/badge/UG-blue) ![](https://img.shields.io/badge/Subject-JPL-blue)
3+
4+
## Role Based Access Control (RBAC)
35

4-
### JPL-XX
6+
### Project Description
57

6-
#### Team Members
8+
<p text-align: justify;>The RBAC project aims to develop a comprehensive Graphical User Interface (GUI) application in Java that facilitates the management of Role-Based Access Control (RBAC) within a system or application. RBAC is a widely used security model that provides a structured approach to managing user permissions and access to system resources.</p>
79

10+
### Module Split-up
11+
| Name | Topic |
12+
|------|-------|
13+
| Seran Pandiyan I P | Home Page |
14+
| Aravind S | Home Page |
15+
| Hem Sagar G H | Login Page |
816

9-
### Deliverables
17+
### Code
1018

19+
```
20+
package com.amrita.jpl.cys21009.project;
21+
/**
22+
* JAVA SEMESTER PROJECT
23+
* simple login system that allows the users to access the database(GUI) according to their roles
24+
*
25+
* RBAC implementation
26+
*
27+
* */
28+
import javax.swing.*;
29+
import javax.swing.table.DefaultTableModel;
30+
import java.awt.*;
31+
import java.awt.event.ActionEvent;
32+
import java.awt.event.ActionListener;
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
import java.util.*;
36+
37+
public class LoginPage extends JFrame {
38+
private JTextField usernameField;
39+
private JPasswordField passwordField;
40+
private Map<String, String> userRoleMap;
41+
42+
public LoginPage() {
43+
setTitle("Login Page");
44+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
45+
setSize(500, 200);
46+
setLocationRelativeTo(null);
47+
48+
// Mapping the users with roles.
49+
userRoleMap = new HashMap<>();
50+
userRoleMap.put("Harry Potter", "manager");
51+
userRoleMap.put("Eren Yeager", "admin");
52+
userRoleMap.put("Percy Jackson", "user");
53+
userRoleMap.put("guest", "guest");
54+
55+
JPanel panel = new JPanel();
56+
panel.setLayout(new GridLayout(3, 2));
57+
58+
JLabel usernameLabel = new JLabel("Username:",SwingConstants.CENTER);
59+
usernameField = new JTextField(20);
60+
61+
JLabel passwordLabel = new JLabel("Password:",SwingConstants.CENTER);
62+
passwordField = new JPasswordField(20);
63+
64+
JButton loginButton = new JButton("Login");
65+
loginButton.addActionListener(new ActionListener() {
66+
@Override
67+
public void actionPerformed(ActionEvent e) {
68+
String username = usernameField.getText();
69+
String password = new String(passwordField.getPassword());
70+
71+
// Check the username and password
72+
if (authenticate(username, password)) {
73+
JOptionPane.showMessageDialog(LoginPage.this, "Login Successful!");
74+
75+
String userRole = userRoleMap.get(username);
76+
openHomePage(userRole);
77+
} else {
78+
JOptionPane.showMessageDialog(LoginPage.this, "Invalid username or password. Please try again.");
79+
}
80+
}
81+
});
82+
83+
panel.add(usernameLabel);
84+
panel.add(usernameField);
85+
panel.add(passwordLabel);
86+
panel.add(passwordField);
87+
panel.add(new JLabel());
88+
panel.add(loginButton);
89+
90+
add(panel);
91+
setVisible(true);
92+
}
93+
94+
private boolean authenticate(String username, String password) {
95+
return username.equals("Eren Yeager") && password.equals("password123")
96+
|| username.equals("Percy Jackson") && password.equals("password456")
97+
|| username.equals("Harry Potter") && password.equals("password000")
98+
|| username.equals("guest") && password.equals("password789");
99+
}
100+
101+
private void openHomePage(String userRole) {
102+
SwingUtilities.invokeLater(new Runnable() {
103+
@Override
104+
public void run() {
105+
new HomePage(userRole);
106+
dispose();
107+
}
108+
});
109+
}
110+
111+
public static void main(String[] args) {
112+
SwingUtilities.invokeLater(new Runnable() {
113+
@Override
114+
public void run() {
115+
new LoginPage();
116+
}
117+
});
118+
}
119+
}
120+
121+
class HomePage extends JFrame {
122+
private String userRole;
123+
private DefaultTableModel tableModel;
124+
private JTable table;
125+
126+
public HomePage(String userRole) {
127+
this.userRole = userRole;
128+
setTitle("Database");
129+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
130+
setSize(800, 600);
131+
setLocationRelativeTo(null);
132+
133+
JPanel panel = new JPanel();
134+
panel.setLayout(new BorderLayout());
135+
136+
// Create a table to display data
137+
String[] columnNames = {"ID", "Name", "Age", "Role"};
138+
Object[][] data = {
139+
{1, "Eren Yeager", 19,"admin"},
140+
{2, "Percy Jackson", 30,"user"},
141+
{3, "Harry Potter", 45,"manager"},
142+
};
143+
tableModel = new DefaultTableModel(data,columnNames);
144+
table = new JTable(tableModel);
145+
JScrollPane scrollPane = new JScrollPane(table);
146+
147+
panel.add(scrollPane, BorderLayout.CENTER);
148+
JPanel buttonpanel = new JPanel(new GridLayout(1,3));
149+
150+
JButton addButton = new JButton("Add Record");
151+
addButton.addActionListener(new ActionListener() {
152+
@Override
153+
public void actionPerformed(ActionEvent e) {
154+
if(userRole.equals("admin")){
155+
addRecord();
156+
157+
} else if (userRole.equals("manager")) {
158+
addRecord();
159+
160+
} else if (userRole.equals("normal")) {
161+
JOptionPane.showMessageDialog(HomePage.this, "You are not permitted to alter the details.");
162+
}
163+
else{
164+
JOptionPane.showMessageDialog(HomePage.this, "You are not permitted to alter the details.");
165+
}
166+
167+
}
168+
});
169+
170+
buttonpanel.add(addButton);
171+
172+
JButton deleteButton = new JButton("Delete Record");
173+
deleteButton.addActionListener(new ActionListener() {
174+
@Override
175+
public void actionPerformed(ActionEvent e) {
176+
if(userRole.equals("admin")){
177+
removeRecord();
178+
}
179+
else{
180+
JOptionPane.showMessageDialog(HomePage.this, "You are not permitted to alter the details.");
181+
}
182+
}
183+
});
184+
buttonpanel.add(deleteButton);
185+
186+
JButton editButton = new JButton("Edit Record");
187+
editButton.addActionListener(new ActionListener() {
188+
@Override
189+
public void actionPerformed(ActionEvent e) {
190+
if(userRole.equals("admin")){
191+
editRecord();
192+
} else if (userRole.equals("manager")) {
193+
editRecord();
194+
System.out.println("Manager is not allowed to edit the roles assigned.");
195+
} else{
196+
JOptionPane.showMessageDialog(HomePage.this, "You are not permitted to alter the details.");
197+
}
198+
199+
}
200+
});
201+
buttonpanel.add(editButton);
202+
panel.add(buttonpanel,BorderLayout.SOUTH);
203+
add(panel);
204+
setVisible(true);
205+
}
206+
private void addRecord() {
207+
JFrame addRecordFrame = new JFrame("Add Record");
208+
addRecordFrame.setSize(300, 400);
209+
addRecordFrame.setLocationRelativeTo(null);
210+
211+
JPanel panel = new JPanel();
212+
panel.setLayout(new GridLayout(5, 2));
213+
214+
JLabel idLabel = new JLabel("ID:");
215+
JTextField idField = new JTextField();
216+
217+
JLabel nameLabel = new JLabel("Name:");
218+
JTextField nameField = new JTextField();
219+
220+
JLabel ageLabel = new JLabel("Age:");
221+
JTextField ageField = new JTextField();
222+
223+
JLabel roleLabel = new JLabel("Role:");
224+
JTextField roleField = new JTextField();
225+
226+
JButton addButton = new JButton("Add");
227+
addButton.addActionListener(new ActionListener() {
228+
@Override
229+
public void actionPerformed(ActionEvent e) {
230+
String id = idField.getText();
231+
String name = nameField.getText();
232+
String age = ageField.getText();
233+
String role = roleField.getText();
234+
235+
Vector<String> record = new Vector<>();
236+
record.add(id);
237+
record.add(name);
238+
record.add(age);
239+
record.add(role);
240+
241+
tableModel.addRow(record);
242+
addRecordFrame.dispose();
243+
}
244+
});
245+
panel.add(idLabel);
246+
panel.add(idField);
247+
panel.add(nameLabel);
248+
panel.add(nameField);
249+
panel.add(ageLabel);
250+
panel.add(ageField);
251+
panel.add(roleLabel);
252+
panel.add(roleField);
253+
panel.add(new JLabel());
254+
panel.add(addButton);
255+
addRecordFrame.add(panel);
256+
addRecordFrame.pack();
257+
addRecordFrame.setVisible(true);
258+
}
259+
private void removeRecord() {
260+
int selectedRow = table.getSelectedRow();
261+
if (selectedRow != -1) {
262+
tableModel.removeRow(selectedRow);
263+
} else {
264+
JOptionPane.showMessageDialog(HomePage.this, "Please select a record to remove.");
265+
}
266+
}
267+
private void editRecord() {
268+
int selectedRow = table.getSelectedRow();
269+
if (selectedRow != -1) {
270+
String id = table.getValueAt(selectedRow, 0).toString();
271+
String name = table.getValueAt(selectedRow, 1).toString();
272+
String age = table.getValueAt(selectedRow, 2).toString();
273+
String role = table.getValueAt(selectedRow,3).toString();
274+
275+
// Create a new frame for editing
276+
JFrame editRecordFrame = new JFrame("Edit Record");
277+
editRecordFrame.setSize(300, 200);
278+
editRecordFrame.setLocationRelativeTo(null);
279+
280+
JPanel panel = new JPanel();
281+
panel.setLayout(new GridLayout(5, 2));
282+
283+
JLabel idLabel = new JLabel("ID:");
284+
JTextField idField = new JTextField(id, 10);
285+
286+
JLabel nameLabel = new JLabel("Name:");
287+
JTextField nameField = new JTextField(name, 20);
288+
289+
JLabel ageLabel = new JLabel("Age:");
290+
JTextField ageField = new JTextField(age, 5);
291+
292+
JLabel roleLabel = new JLabel("Role:");
293+
JTextField roleField = new JTextField(role,5);
294+
295+
JButton saveButton = new JButton("Save");
296+
saveButton.addActionListener(new ActionListener() {
297+
@Override
298+
public void actionPerformed(ActionEvent e) {
299+
String newId = idField.getText();
300+
String newName = nameField.getText();
301+
String newAge = ageField.getText();
302+
String newRole = roleField.getText().toLowerCase();
303+
304+
table.setValueAt(newId, selectedRow, 0);
305+
table.setValueAt(newName, selectedRow, 1);
306+
table.setValueAt(newAge, selectedRow, 2);
307+
table.setValueAt(newRole, selectedRow, 3);
308+
309+
editRecordFrame.dispose();
310+
}
311+
});
312+
313+
panel.add(idLabel);
314+
panel.add(idField);
315+
panel.add(nameLabel);
316+
panel.add(nameField);
317+
panel.add(ageLabel);
318+
panel.add(ageField);
319+
if(!userRole.equals("manager")){
320+
panel.add(roleLabel);
321+
panel.add(roleField);
322+
}
323+
panel.add(new JLabel());
324+
panel.add(saveButton);
325+
326+
editRecordFrame.add(panel);
327+
editRecordFrame.setVisible(true);
328+
} else {
329+
JOptionPane.showMessageDialog(HomePage.this, "Please select a record to edit.");
330+
}
331+
}
332+
}
333+
334+
335+
```
336+
337+
### Demo
338+
#### Screenshots
339+
340+
<p align="center">
341+
<img src = "Assets/S1.jpg" width=600>
342+
<img src = "Assets/S2.jpg" width=600>
343+
<img src = "Assets/S3.jpg" width=600>
344+
<img src = "Assets/S4.jpg" width=600>
345+
<img src = "Assets/S5.jpg" width=600>
346+
<img src = "Assets/S6.jpg" width=600>
347+
<img src = "Assets/S7.jpg" width=600>
348+
<img src = "Assets/S8.jpg" width=600>
349+
<img src = "Assets/S9.jpg" width=600>
350+
<img src = "Assets/S10.jpg" width=600>
351+
<img src = "Assets/S11.jpg" width=600>
352+
</p>
353+
354+
#### Account with "Admin" access
355+
356+
<p align="center">
357+
<img src = "Assets/demo1.gif" width=600>
358+
</p>
359+
360+
#### Account with "User" access
361+
362+
<p align="center">
363+
<img src = "Assets/demo2.gif" width=600>
364+
</p>

0 commit comments

Comments
 (0)