19、权限管理练习

(3) 2024-08-30 15:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
19、权限管理练习,希望能够帮助你!!!。

练习

分别有权限表,用户表,用户对应权限表三个表

需求:

1、用户登录验证帐号密码;

2、查看自己的权限;

权限管理练习:

权限表:

1、订单管理

2、用户管理

3、菜单管理

4、权限分配

5、BUG管理

用户表:

1、cce 1

2、csw 2

分配表:

1、 1 3

2、 2 1


表结构创建

drop table if exists allocation;

drop table if exists permission;

create table permission(id int auto_increment,pname char(32) not null,primary key(id)) engine=innodb default charset=utf8;

insert into permission(pname) values('订单管理'),('用户管理'),('菜单管理'),('权限分配'),('BUG管理');

drop table if exists users;

create table users(id int auto_increment,username char(32) not null,passwd char(32) not null,primary key(id),unique key(username)) engine=innodb default charset=utf8;

insert into users(username,passwd) values ('cce','caichangen'),('csw','caishuiwang');

create table allocation(id int auto_increment,user_id int,permission_id int,primary key(id),unique key(user_id,permission_id),foreign key(permission_id) references permission(id),foreign key(user_id) references users(id));

insert into allocation(user_id,permission_id) values (1,3),(2,1);

19、权限管理练习_https://bianchenghao6.com/blog__第1张

实现代码

# mysql工具类

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @Time : 2018/7/6 15:59

# @Author : CaiChangEn

# @Email : mail0426@163.com

# @Software: PyCharm

import pymysql

class mysqldb:

def __init__(self, dbhost, dbport, dbuser, dbpassword, dbdatabase):

self.dbhost = dbhost

self.dbport = dbport

self.dbuser = dbuser

self.dbpassword = dbpassword

self.dbdatabase = dbdatabase

self.connect()

def connect(self):

self.conn = pymysql.connect(host=self.dbhost, port=self.dbport, user=self.dbuser, password=self.dbpassword,

database=self.dbdatabase, charset='utf8')

self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

def selected(self, statement):

action, *_ = statement.split(' ')

if hasattr(self, action):

func = getattr(self, action)

return func(statement)

def select(self, statement):

self.cursor.execute(statement)

data = self.cursor.fetchall()

if data:

return data[0]

else:

raise ValueError("The query is wrong")

def update(self, statement):

self.insert(statement)

def alter(self, statement):

self.insert(statement)

def insert(self, statement):

try:

self.cursor.execute(statement)

return self.conn.commit()

except Exception:

self.conn.rollback()

def close(self):

self.cursor.close()

self.conn.close()

if __name__ == '__main__':

conn = mysqldb(host, port, username, password, database)

result = conn.selected('select * from users where id=1')

# result = conn.selected("update users set passwd='caichangen' where id='1' ")

# result = conn.selected("alter table users add vae int(11) ")

# result = conn.selected("insert into users(username,passwd) values ('cfj','caifengjun')")

print(result)

conn.close()

# 逻辑代码

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

from mysqldb import *

host = '127.0.0.1'

port = 3306

username = 'root'

password = ''

database = 'cce'

conn = mysqldb(host, port, username, password, database)

def program():

count = 0

while count<3:

username=input('请输入账号:')

passwd=input('请输入密码:')

if not username or not passwd:count += 1 ;continue

data=conn.selected("select * from users where username='%s'" %username)

if username != data['username'] or passwd != data['passwd']:

print('Error')

count += 1

else:

choice=''

while choice.lower() !='q':

choice = input('1:查看权限;(q/Q):退出:')

if not choice:continue

if choice == '1':

data = conn.selected("select pname from permission where id in (select permission_id from allocation where user_id in (select id from users where username='%s'));" %username)

for i in data.values():

print(i)

else:

break

program()

conn.close()

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复