-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponent.js
More file actions
106 lines (93 loc) · 3.7 KB
/
Copy pathComponent.js
File metadata and controls
106 lines (93 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
sap.ui.define([
"sap/ui/core/UIComponent",
"sap/ui/Device",
"sap/ui/model/json/JSONModel",
"sap/base/Log"
], function (UIComponent, Device, JSONModel, Log) {
"use strict";
return UIComponent.extend("sap.employeeapp.Component", {
metadata: {
manifest: "json",
interfaces: ["sap.ui.core.IAsyncContentCreation"]
},
init: function () {
try {
UIComponent.prototype.init.apply(this, arguments);
// Set explicit component ID
this.getComponentId();
this._initializeRouter();
this._setDeviceModel();
this._setEmployeeModel();
Log.info("Component initialized successfully");
} catch (oError) {
Log.error("Component initialization failed: " + oError.message);
throw oError;
}
},
getComponentId: function () {
return this.getId();
},
_initializeRouter: function () {
if (this.getRouter()) {
// this.getRouter().attachBeforeRouteMatched(this._checkAuth, this);
this.getRouter().attachRouteMatched(this._checkAuth, this);
this.getRouter().initialize();
Log.debug("Router initialized");
} else {
Log.warning("No router configuration found in manifest");
}
},
_checkAuth: function (oEvent) {
var sRouteName = oEvent.getParameter("name");
var bLoggedIn = localStorage.getItem("isLoggedIn") === "true";
var userRole = localStorage.getItem("userRole"); // 👈 get role
// If not logged in, allow only "login" route
if (!bLoggedIn && sRouteName !== "login") {
this.getRouter().navTo("login", {}, true);
return;
}
// If logged in, prevent navigating back to login
if (bLoggedIn && sRouteName === "login") {
if (userRole === "admin") {
this.getRouter().navTo("home", {}, true);
} else if (userRole === "employee") {
this.getRouter().navTo("employeeMain", {}, true);
}
return;
}
// 🔒 Role-based restrictions
if (userRole === "employee" && sRouteName === "home") {
// Employee trying to access admin route
this.getRouter().navTo("employeeMain", {}, true);
return;
}
if (userRole === "admin" && sRouteName === "employeeMain") {
// Admin trying to access employee-only route
this.getRouter().navTo("home", {}, true);
return;
}
},
_setDeviceModel: function () {
var oDeviceModel = new JSONModel(Device);
oDeviceModel.setDefaultBindingMode("OneWay");
this.setModel(oDeviceModel, "device");
},
_setEmployeeModel: function () {
var oEmployeeData = {
Employee: { name: "", email: "" },
Work: { department: "", role: "" }
};
var oEmployeeModel = new JSONModel(oEmployeeData);
this.setModel(oEmployeeModel, "employeeModel");
},
destroy: function () {
UIComponent.prototype.destroy.apply(this, arguments);
},
getContentDensityClass: function () {
if (!this._sContentDensityClass) {
this._sContentDensityClass = Device.support.touch ? "sapUiSizeCozy" : "sapUiSizeCompact";
}
return this._sContentDensityClass;
}
});
});