JanviMl commited on
Commit
ab4032a
·
verified ·
1 Parent(s): 1af090e

Create auth_system.py

Browse files
Files changed (1) hide show
  1. src/auth_system.py +128 -0
src/auth_system.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import hashlib
2
+ from typing import Dict, Optional
3
+
4
+ class AuthSystem:
5
+ """Authentication and role management system"""
6
+
7
+ def __init__(self):
8
+ # Demo users with hashed passwords
9
+ # In production, this would be stored in a secure database
10
+ self.users = {
11
+ "tony.finance": {
12
+ "password_hash": self._hash_password("password123"),
13
+ "role": "Finance",
14
+ "full_name": "Tony Sharma",
15
+ "department": "Finance"
16
+ },
17
+ "sarah.marketing": {
18
+ "password_hash": self._hash_password("password123"),
19
+ "role": "Marketing",
20
+ "full_name": "Sarah Johnson",
21
+ "department": "Marketing"
22
+ },
23
+ "mike.hr": {
24
+ "password_hash": self._hash_password("password123"),
25
+ "role": "HR",
26
+ "full_name": "Mike Wilson",
27
+ "department": "Human Resources"
28
+ },
29
+ "peter.engineering": {
30
+ "password_hash": self._hash_password("password123"),
31
+ "role": "Engineering",
32
+ "full_name": "Peter Pandey",
33
+ "department": "Engineering"
34
+ },
35
+ "ceo.admin": {
36
+ "password_hash": self._hash_password("password123"),
37
+ "role": "C-Level",
38
+ "full_name": "CEO Admin",
39
+ "department": "Executive"
40
+ },
41
+ "john.employee": {
42
+ "password_hash": self._hash_password("password123"),
43
+ "role": "Employee",
44
+ "full_name": "John Doe",
45
+ "department": "General"
46
+ }
47
+ }
48
+
49
+ # Role-based access permissions - using embedded document content identifiers
50
+ self.role_permissions = {
51
+ "Finance": {
52
+ "documents": ["financial_reports", "expense_data", "budget_info"],
53
+ "keywords": ["revenue", "financial", "profit", "sales", "budget", "expenses", "cost"],
54
+ "description": "Access to financial reports, budgets, and expense data"
55
+ },
56
+ "Marketing": {
57
+ "documents": ["marketing_reports", "campaign_data", "customer_metrics"],
58
+ "keywords": ["marketing", "campaign", "customer", "roi", "acquisition", "conversion"],
59
+ "description": "Access to marketing campaigns, performance metrics, and customer data"
60
+ },
61
+ "HR": {
62
+ "documents": ["employee_data", "hr_policies", "attendance_records"],
63
+ "keywords": ["employee", "hr", "policy", "leave", "attendance", "benefits", "payroll"],
64
+ "description": "Access to employee data, policies, and HR processes"
65
+ },
66
+ "Engineering": {
67
+ "documents": ["technical_docs", "architecture", "development_processes"],
68
+ "keywords": ["architecture", "technology", "system", "development", "engineering", "technical"],
69
+ "description": "Access to technical documentation and system architecture"
70
+ },
71
+ "C-Level": {
72
+ "documents": ["financial_reports", "marketing_reports", "employee_data", "technical_docs", "all_data"],
73
+ "keywords": ["all", "company", "overview", "performance", "metrics", "strategy"],
74
+ "description": "Full access to all company data and reports"
75
+ },
76
+ "Employee": {
77
+ "documents": ["general_policies", "company_info", "benefits"],
78
+ "keywords": ["policy", "benefits", "company", "general", "handbook", "leave"],
79
+ "description": "Access to general company policies and employee information"
80
+ }
81
+ }
82
+
83
+ def _hash_password(self, password: str) -> str:
84
+ """Hash password using SHA-256"""
85
+ return hashlib.sha256(password.encode()).hexdigest()
86
+
87
+ def authenticate(self, username: str, password: str) -> bool:
88
+ """Authenticate user credentials"""
89
+ if username not in self.users:
90
+ return False
91
+
92
+ password_hash = self._hash_password(password)
93
+ return self.users[username]["password_hash"] == password_hash
94
+
95
+ def get_user_role(self, username: str) -> Optional[str]:
96
+ """Get user role"""
97
+ if username not in self.users:
98
+ return None
99
+ return self.users[username]["role"]
100
+
101
+ def get_user_info(self, username: str) -> Optional[Dict]:
102
+ """Get user information"""
103
+ if username not in self.users:
104
+ return None
105
+ user_info = self.users[username].copy()
106
+ # Remove password hash for security
107
+ del user_info["password_hash"]
108
+ return user_info
109
+
110
+ def get_role_permissions(self, role: str) -> Dict:
111
+ """Get permissions for a specific role"""
112
+ return self.role_permissions.get(role, {})
113
+
114
+ def can_access_content(self, role: str, content_type: str) -> bool:
115
+ """Check if a role can access specific content"""
116
+ permissions = self.get_role_permissions(role)
117
+ allowed_docs = permissions.get("documents", [])
118
+ return content_type in allowed_docs or "all_data" in allowed_docs
119
+
120
+ def get_accessible_documents(self, role: str) -> list:
121
+ """Get list of documents accessible to a role"""
122
+ permissions = self.get_role_permissions(role)
123
+ return permissions.get("documents", [])
124
+
125
+ def get_role_keywords(self, role: str) -> list:
126
+ """Get keywords relevant to a role for better content filtering"""
127
+ permissions = self.get_role_permissions(role)
128
+ return permissions.get("keywords", [])