This Python-based UserManagement
class provides a framework for managing user accounts, roles, and permissions within an SQLite database. It allows for operations such as adding users, assigning roles, validating permissions, and managing user information and roles in a scalable and structured manner.
UserManagement
class in your project folder.UserManagement
class is instantiated.The system creates the following tables:
users
)id
: Auto-incrementing primary key.sub
: User's unique identifier (must be unique).name
: User's full name.picture
: URL of the user's profile picture.email
: User's email address.created_at
: Timestamp when the user was created.updated_at
: Timestamp when the user was last updated.permissions
)id
: Auto-incrementing primary key.role
: Role name (e.g., 'admin', 'user').priority
: Role's priority level (lower numbers represent higher authority).created_at
: Timestamp when the permission was created.updated_at
: Timestamp when the permission was last updated.roles
)id
: Auto-incrementing primary key.sub
: User's unique identifier (foreign key referencing users
table).role
: Role name (foreign key referencing permissions
table).created_at
: Timestamp when the role was assigned to the user.updated_at
: Timestamp when the role was last updated.__init__(db_file)
Initializes the class and creates the database tables if they don't already exist.
get_connection()
Creates and returns a new SQLite database connection.
create_tables()
Creates the necessary database tables (users
, permissions
, roles
).
add_permission(role, priority)
Adds a new permission/role to the permissions
table.
role
(str): Name of the role.priority
(int): Priority level of the role.assign_role(sub, role)
Assigns a role to a user.
sub
(str): User's unique identifier.role
(str): Role to assign.validate_user_permission(sub, required_role)
Validates if a user has a specific role or higher.
sub
(str): User's unique identifier.required_role
(str): The role to validate against.insert_user(sub, name, email, picture=None)
Inserts a new user or updates the existing user if changes are detected.
sub
(str): User's unique identifier.name
(str): Full name of the user.email
(str): User's email address.picture
(str): URL of the user's profile picture (optional).get_user_by_id(user_id)
Fetches a user's information by their unique identifier.
user_id
(str): The unique identifier of the user.get_user_roles(user_id)
Fetches all roles assigned to a user.
user_id
(str): The unique identifier of the user.check_email_exists(email)
Checks if an email address already exists in the system.
email
(str): The email address to check.fetch_users()
Fetches all user information from the users
table.
get_fetch_available_roles()
Fetches all available roles and their priorities from the permissions
table.
get_fetch_roles_for_user(sub)
Fetches the roles assigned to a specific user.
sub
(str): The unique identifier of the user.remove_role(user_id, role)
Removes a specific role from a user.
user_id
(str): The unique identifier of the user.role
(str): The role to be removed.Here are some examples of how to use the UserManagement
class:
The system handles errors such as:
Each database connection is closed after the operation to prevent connection leaks.
This UserManagement
class is a robust solution for managing users and permissions in applications where role-based access control is required. It is scalable, easy to use, and can be integrated into Flask or other Python-based web applications.