This commit is contained in:
yumoqing 2024-11-14 15:12:34 +08:00
parent fb27fdb95d
commit bfedcb2977
4 changed files with 91 additions and 3 deletions

View File

@ -6,13 +6,14 @@ from appPublic.registerfunction import RegisterFunction
from appPublic.rc4 import password, unpassword from appPublic.rc4 import password, unpassword
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
from appPublic.log import debug, exception from appPublic.log import debug, exception
from ahserver.auth_api import AuthAPI from ahserver.auth_api import AuthAPI, user_login
from ahserver.serverenv import ServerEnv from ahserver.serverenv import ServerEnv
import jwt import jwt
defualt_password_key = "!@#$%^&*(*&^%$QWERTYUIqwertyui234567" defualt_password_key = "!@#$%^&*(*&^%$QWERTYUIqwertyui234567"
defualt_secret_key="test1234!@#$2222354" defualt_secret_key="test1234!@#$2222354"
def generate_token(ak): def generate_token(ak):
tim = int(round(time.time())) tim = int(round(time.time()))
exp = tim + 5 * 60 exp = tim + 5 * 60
@ -39,6 +40,18 @@ def get_apikey_from_token(token):
except: except:
return None return None
async def get_user_roles():
userid = await get_user()
sql = "select b.name from userrole a, role b where a.userid=${userid}$ and a.roleid = b.id"
db = DBPools()
roles = []
async with db.sqlorContext(dbname) as sor:
recs = sor.sqlExe(sql, {'userid':userid})
if len(recs) < 1:
return roles
for r in recs:
roles.append(r.name)
return roles
def mypassword(passwd): def mypassword(passwd):
config = getConfig() config = getConfig()
@ -51,9 +64,31 @@ def myunpassword(passwd):
return unpassword(passwd) return unpassword(passwd)
def load_rbac(): def load_rbac():
AuthAPI.checkUserPermission = objcheckperm
env = ServerEnv() env = ServerEnv()
env.password = mypassword env.password = mypassword
env.unpassword = myunpassword env.unpassword = myunpassword
env.get_user_roles = get_user_roles
env.check_user_password = checkUserPassword
async def get_dbname():
rf = RegisterFunction()
dbname = await rf.exe('get_module_dbname', 'rbac')
return dbname
async def checkUserPassword(request, username, password):
db = DBPools()
dbname = await get_dbname()
async with db.sqlorContext(dbname) as sor:
sql = "select * from users where username=${username}$ and password=${password}$"
recs = await sor.sqlExe(sql, {'username':username, 'password':password})
if len(recs) < 1:
return False
await user_login(request, recs[0].id,
username=recs[0].username,
userorgid=recs[0].orgid)
return True
return False
async def getAuthenticationUserid(sor, request): async def getAuthenticationUserid(sor, request):
auth = request.headers.get('Authentication') auth = request.headers.get('Authentication')
@ -115,5 +150,3 @@ and c.del_flg='0'"""
debug(f'error happened {userid}, {path}') debug(f'error happened {userid}, {path}')
return False return False
AuthAPI.checkUserPermission = objcheckperm

View File

@ -1 +1,2 @@
aiohttp aiohttp
PyJWT

View File

@ -0,0 +1,11 @@
username = params_kw.get('username')
passwd = params_kw.get('password')
if not passwd:
return Error(title='Login failed', message='Password is required')
passwd = password(passwd)
rzt = await check_user_password(username, passwd):
if rzt:
return Message(title='Logined', message=f'Welcome back {get_usernme()}')
return Error(title='login failed', message='user and password mismatch')

View File

@ -0,0 +1,43 @@
{
"id":"login_window",
"widgettype":"PopupWindow",
"options":{
"auto_open":true,
"anthor":"cc",
"width":"70%",
"height":"70%"
},
"subwidgets":[
{
"widgettype":"Form",
"id":"userpasswd",
"options":{
"cols":1,
"fields":[
{
"name":"username",
"label":"用户名",
"uitype":"str"
},
{
"name":"passwd",
"label":"密码",
"uitype":"password"
}
]
}
}
],
"binds":[
{
"wid":"userpasswd",
"event":"submit",
"actiontype":"urlwidget",
"target":"self",
"options":{
"url":"{{entire_url('userpassword_login.dspy')}}"
}
}
]
}