def authenticate(self, username, password):
for user in self.users:
if user.username == username and user.check_password(password):
return True
return False
def perform_action(self, username, password, action):
if self.authenticate(username, password):
# Check user role to determine access level
for user in self.users:
if user.username == username:
if user.role == 'admin':
# Allow admin to perform any action
print(f"Admin {username} performed action: {action}")
elif user.role == 'operator':
# Allow operator to perform specific actions
if action in ['read', 'write']:
print(f"Operator {username} performed action: {action}")
else:
print("Operator does not have permission to perform this action.")
else:
print("Unknown user role.")
else:
print("Authentication failed.")
# Example usage
system = System()
# Create users with different roles and passwords
admin = User("admin", "admin123", "admin")
operator = User("operator", "op123", "operator")
# Add users to the system
system.add_user(admin)
system.add_user(operator)
# Attempt to perform actions with correct credentials
system.perform_action("admin", "admin123", "delete") # Should allow as admin
system.perform_action("operator", "op123", "read") # Should allow as operator
system.perform_action("operator", "wrongpass", "write") # Should fail authentication
system.perform_action("unknown", "unknownpass", "read") # Should fail authentication
```