File size: 5,714 Bytes
ab4032a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import hashlib
from typing import Dict, Optional

class AuthSystem:
    """Authentication and role management system"""
    
    def __init__(self):
        # Demo users with hashed passwords
        # In production, this would be stored in a secure database
        self.users = {
            "tony.finance": {
                "password_hash": self._hash_password("password123"),
                "role": "Finance",
                "full_name": "Tony Sharma",
                "department": "Finance"
            },
            "sarah.marketing": {
                "password_hash": self._hash_password("password123"),
                "role": "Marketing",
                "full_name": "Sarah Johnson",
                "department": "Marketing"
            },
            "mike.hr": {
                "password_hash": self._hash_password("password123"),
                "role": "HR",
                "full_name": "Mike Wilson",
                "department": "Human Resources"
            },
            "peter.engineering": {
                "password_hash": self._hash_password("password123"),
                "role": "Engineering",
                "full_name": "Peter Pandey",
                "department": "Engineering"
            },
            "ceo.admin": {
                "password_hash": self._hash_password("password123"),
                "role": "C-Level",
                "full_name": "CEO Admin",
                "department": "Executive"
            },
            "john.employee": {
                "password_hash": self._hash_password("password123"),
                "role": "Employee",
                "full_name": "John Doe",
                "department": "General"
            }
        }
        
        # Role-based access permissions - using embedded document content identifiers
        self.role_permissions = {
            "Finance": {
                "documents": ["financial_reports", "expense_data", "budget_info"],
                "keywords": ["revenue", "financial", "profit", "sales", "budget", "expenses", "cost"],
                "description": "Access to financial reports, budgets, and expense data"
            },
            "Marketing": {
                "documents": ["marketing_reports", "campaign_data", "customer_metrics"],
                "keywords": ["marketing", "campaign", "customer", "roi", "acquisition", "conversion"],
                "description": "Access to marketing campaigns, performance metrics, and customer data"
            },
            "HR": {
                "documents": ["employee_data", "hr_policies", "attendance_records"],
                "keywords": ["employee", "hr", "policy", "leave", "attendance", "benefits", "payroll"],
                "description": "Access to employee data, policies, and HR processes"
            },
            "Engineering": {
                "documents": ["technical_docs", "architecture", "development_processes"],
                "keywords": ["architecture", "technology", "system", "development", "engineering", "technical"],
                "description": "Access to technical documentation and system architecture"
            },
            "C-Level": {
                "documents": ["financial_reports", "marketing_reports", "employee_data", "technical_docs", "all_data"],
                "keywords": ["all", "company", "overview", "performance", "metrics", "strategy"],
                "description": "Full access to all company data and reports"
            },
            "Employee": {
                "documents": ["general_policies", "company_info", "benefits"],
                "keywords": ["policy", "benefits", "company", "general", "handbook", "leave"],
                "description": "Access to general company policies and employee information"
            }
        }
    
    def _hash_password(self, password: str) -> str:
        """Hash password using SHA-256"""
        return hashlib.sha256(password.encode()).hexdigest()
    
    def authenticate(self, username: str, password: str) -> bool:
        """Authenticate user credentials"""
        if username not in self.users:
            return False
        
        password_hash = self._hash_password(password)
        return self.users[username]["password_hash"] == password_hash
    
    def get_user_role(self, username: str) -> Optional[str]:
        """Get user role"""
        if username not in self.users:
            return None
        return self.users[username]["role"]
    
    def get_user_info(self, username: str) -> Optional[Dict]:
        """Get user information"""
        if username not in self.users:
            return None
        user_info = self.users[username].copy()
        # Remove password hash for security
        del user_info["password_hash"]
        return user_info
    
    def get_role_permissions(self, role: str) -> Dict:
        """Get permissions for a specific role"""
        return self.role_permissions.get(role, {})
    
    def can_access_content(self, role: str, content_type: str) -> bool:
        """Check if a role can access specific content"""
        permissions = self.get_role_permissions(role)
        allowed_docs = permissions.get("documents", [])
        return content_type in allowed_docs or "all_data" in allowed_docs
    
    def get_accessible_documents(self, role: str) -> list:
        """Get list of documents accessible to a role"""
        permissions = self.get_role_permissions(role)
        return permissions.get("documents", [])
    
    def get_role_keywords(self, role: str) -> list:
        """Get keywords relevant to a role for better content filtering"""
        permissions = self.get_role_permissions(role)
        return permissions.get("keywords", [])