registre-permissions/main.py
2023-09-24 12:58:19 -04:00

138 lines
4.3 KiB
Python

#!/usr/bin/env python
# A large proportion of this code is taken from the peewee project.
# This code is made available under the MIT License
# https://github.com/coleifer/peewee/tree/master
from collections import OrderedDict
from getpass import getpass
import sys
import data_models as dm
from crud import *
def initialize(db_passphrase):
dm.db.init("permissions.db", passphrase=db_passphrase)
list_table_models = [dm.Person,
dm.Resource,
dm.Role,
dm.Permission,
dm.UserAccount]
dm.db.create_tables(list_table_models)
def menu_loop():
menu_choice = None
while menu_choice != 'q':
for key, value in menu.items():
print('%s) %s' % (key, value[1]))
menu_choice = input('Action: ').lower().strip()
if menu_choice in menu:
menu[menu_choice][0]()
def person_menu_loop():
person_menu_choice = None
while person_menu_choice != 'q':
for key, value in person_menu.items():
print('%s) %s' % (key, value[1]))
person_menu_choice = input('Action: ').lower().strip()
if person_menu_choice in person_menu:
person_menu[person_menu_choice][0]()
def resource_menu_loop():
resource_menu_choice = None
while resource_menu_choice != 'q':
for key, value in resource_menu.items():
print('%s) %s' % (key, value[1]))
resource_menu_choice = input('Action: ').lower().strip()
if resource_menu_choice in resource_menu:
resource_menu[resource_menu_choice][0]()
def role_menu_loop():
role_menu_choice = None
while role_menu_choice != 'q':
for key, value in role_menu.items():
print('%s) %s' % (key, value[1]))
role_menu_choice = input('Action: ').lower().strip()
if role_menu_choice in role_menu:
role_menu[role_menu_choice][0]()
def permission_menu_loop():
permission_menu_choice = None
while permission_menu_choice != 'q':
for key, value in permission_menu.items():
print('%s) %s' % (key, value[1]))
permission_menu_choice = input('Action: ').lower().strip()
if permission_menu_choice in permission_menu:
permission_menu[permission_menu_choice][0]()
def user_account_menu_loop():
user_account_menu_choice = None
while user_account_menu_choice != 'q':
for key, value in user_account_menu.items():
print('%s) %s' % (key, value[1]))
user_account_menu_choice = input('Action: ').lower().strip()
if user_account_menu_choice in user_account_menu:
user_account_menu[user_account_menu_choice][0]()
menu = OrderedDict([
('1', (person_menu_loop, "Person")),
('2', (resource_menu_loop, "Resource")),
('3', (role_menu_loop, "Role")),
('4', (permission_menu_loop, "Permission")),
('5', (user_account_menu_loop, "User Account"))
])
person_menu = OrderedDict([
('1', (add_person, "Add Person")),
('2', (list_person, "List Person")),
('3', (edit_person, "Edit Person")),
('4', (delete_person, "Delete Person")),
])
resource_menu = OrderedDict([
('1', (add_resource, "Add Resource")),
('2', (list_resource, "List Resources")),
('3', (edit_resource, "Edit Resource")),
('4', (delete_resource, "Delete Resource")),
])
role_menu = OrderedDict([
('1', (add_role, "Add Role")),
('2', (list_role, "List Roles")),
('3', (edit_role, "Edit Role")),
('4', (delete_role, "Delete Role")),
])
permission_menu = OrderedDict([
('1', (add_permission, "Add Permission")),
('2', (list_permission, "List Permissions")),
('3', (edit_permission, "Edit Permission")),
('4', (delete_permission, "Delete Permission")),
])
user_account_menu = OrderedDict([
('1', (add_user_account, "Add User Account")),
('2', (list_user_account, "List User Accounts")),
('3', (edit_user_account, "Edit User Account")),
('4', (delete_user_account, "Delete User Account")),
])
if __name__ == '__main__':
# Collect the passphrase using a secure method.
passphrase = getpass('Entre le mot de passe: ')
if not passphrase:
sys.stderr.write('Un mot de passe est requis pour accéder à la base de données.\n')
sys.stderr.flush()
sys.exit(1)
# Initialize the database.
initialize(passphrase)
menu_loop()