{"id":27299959,"url":"https://github.com/parisaalizadeh2003/accesscontrol-","last_synced_at":"2025-04-12T00:50:12.252Z","repository":{"id":283118069,"uuid":"950746976","full_name":"ParisaAlizadeh2003/AccessControl-","owner":"ParisaAlizadeh2003","description":"This project implements an authentication decorator to control user access levels in Python. Based on roles (Admin, Member, Guest), it determines whether a user is authorized to execute a function. 🚀🔒","archived":false,"fork":false,"pushed_at":"2025-03-18T16:18:38.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T00:50:04.229Z","etag":null,"topics":["access-control","admin","authentication","coding","decorators","guest","member","permissions","programming","python","python-security","rbac","role-based-access","security","user-authentication"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ParisaAlizadeh2003.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-18T16:16:34.000Z","updated_at":"2025-03-18T16:21:10.000Z","dependencies_parsed_at":"2025-03-18T17:43:27.562Z","dependency_job_id":null,"html_url":"https://github.com/ParisaAlizadeh2003/AccessControl-","commit_stats":null,"previous_names":["parisaalizadeh2003/accesscontrol-"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisaAlizadeh2003%2FAccessControl-","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisaAlizadeh2003%2FAccessControl-/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisaAlizadeh2003%2FAccessControl-/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisaAlizadeh2003%2FAccessControl-/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ParisaAlizadeh2003","download_url":"https://codeload.github.com/ParisaAlizadeh2003/AccessControl-/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248501902,"owners_count":21114681,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["access-control","admin","authentication","coding","decorators","guest","member","permissions","programming","python","python-security","rbac","role-based-access","security","user-authentication"],"created_at":"2025-04-12T00:50:11.683Z","updated_at":"2025-04-12T00:50:12.237Z","avatar_url":"https://github.com/ParisaAlizadeh2003.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Authentication Decorator 🔒\n\n## Overview  \nThis project implements an **authentication decorator** in Python to control access to functions based on user roles. The decorator ensures that only authorized users can execute certain functions while restricting access for others.  \n\n## Features  \n- Role-based access control using an **Enum** (`Admin`, `Member`, `Guest`)  \n- Restricts unauthorized users from executing protected functions  \n- Raises an exception for invalid roles  \n- Demonstrates authentication logic with example functions  \n\n## Installation  \nNo external dependencies are required. Ensure you have **Python 3.x** installed.  \n\n## Usage  \n### 1. Define User Roles  \nRoles are defined using an **Enum** to enforce predefined user access levels:  \n\n```python\nimport enum\n\nclass UserRoles(enum.Enum):\n    Admin = \"admin\"\n    Member = \"member\"\n    Guest = \"guest\"\n```\n\n### 2. Implement the Authentication Decorator  \nThe `authentication` decorator checks the user's role before allowing access to the function:  \n\n```python\ndef authentication(user_role):\n    def decorator(func):\n        def wrapper(*args, **kwargs):\n            if user_role == UserRoles.Admin:\n                return func(*args, **kwargs)\n            elif user_role == UserRoles.Member:\n                return \"Access Denied: You are a Member.\"\n            elif user_role == UserRoles.Guest:\n                return \"Access Denied: You are a Guest.\"\n            else:\n                raise PermissionError(\"Invalid Role!\")\n        return wrapper\n    return decorator\n```\n\n### 3. Apply the Decorator to Functions  \nExample functions that require authentication:  \n\n```python\n@authentication(UserRoles.Admin)\ndef delete_user(user_id):\n    return f\"User {user_id} has been successfully deleted.\"\n\n@authentication(UserRoles.Member)\ndef view_profile():\n    return \"Displaying user profile.\"\n```\n\n## Running Tests  \nTo test the authentication logic, use **pytest**.  \n\n### Test Script  \nCreate a test file `test_authentication.py`:  \n\n```python\nimport pytest\nfrom authentication_decorator import authentication, UserRoles\n\n@authentication(UserRoles.Admin)\ndef admin_task():\n    return \"Admin task executed.\"\n\n@authentication(UserRoles.Member)\ndef member_task():\n    return \"Member task executed.\"\n\n@authentication(UserRoles.Guest)\ndef guest_task():\n    return \"Guest task executed.\"\n\ndef test_admin_access():\n    assert admin_task() == \"Admin task executed.\"\n\ndef test_member_access():\n    assert member_task() == \"Access Denied: You are a Member.\"\n\ndef test_guest_access():\n    assert guest_task() == \"Access Denied: You are a Guest.\"\n\ndef test_invalid_role():\n    with pytest.raises(PermissionError, match=\"Invalid Role!\"):\n        @authentication(\"unknown_role\")\n        def invalid_task():\n            pass\n        invalid_task()\n```\n\nRun tests using:  \n```bash\npytest test_authentication.py\n```\n\n## License  \nThis project is licensed under the **MIT License**.  \n\n## Contributing  \nFeel free to open issues or submit pull requests! 🎉  \n\n---\n\n### 🚀 Stay Secure \u0026 Control Access with Python!  \n```\n\nThis README provides **clear explanations, usage examples, and a test section**. Let me know if you need any modifications! 🚀\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparisaalizadeh2003%2Faccesscontrol-","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparisaalizadeh2003%2Faccesscontrol-","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparisaalizadeh2003%2Faccesscontrol-/lists"}