def top(): # return {'message':ut.session_get('message', True), # 'role':Auth.get_role()} return template('index', uid=ut.session_get('uid'), name="main1", redirectUrl=redirectUrl, appUrl=appUrl, message=ut.session_get('message', True), role=Auth.get_role())
def top2(): return template('index2', name="main2", redirectUrl=redirectUrl, appUrl=appUrl, message=ut.session_get('message', True), role=Auth.get_role())
def change_pw(): uid = ut.session_get('uid') if ut.form_get('user'): uid = ut.form_get('user') current_pw, new_pw1, new_pw2 = [ ut.form_get(x) for x in ('current_pw', 'new_pw1', 'new_pw2') ] #print("current_pw="+current_pw+" new_pw1="+new_pw1+" new_pw2="+ new_pw2) #管理者の場合はカレントパスワード不要 if Auth.get_role() == 'admin': b, msg = ut.change_pw_super(uid, new_pw1, new_pw2) else: b, msg = ut.change_pw(uid, current_pw, new_pw1, new_pw2) if b: #ut.session_set('message', msg) if ut.form_get('from_show_users') == "1": return template('users', users=ut.users(), appUrl=appUrl, message=msg, role=Auth.get_role()) else: btl.redirect(redirectUrl) else: return template('change_pw', appUrl=appUrl, message=msg, role=Auth.get_role())
def change_pw_form(): return template('change_pw', appUrl=appUrl, message=ut.session_get('message', True), role=Auth.get_role(), user=ut.form_get('user'), from_show_users=ut.form_get('from_show_users'))
class Auth: '''generating decorators for access control''' # class attribute CLS_ATTR=[ ('get_role_from_db', lambda uid,pw:None), # lambda uid,pw: role if a record having (uid, pw) exists in the db else None ('logout', ut.logout), # method to clear the session ('set_uid', partial(ut.session_set, 'uid')), # method to set uid into the session ('set_role', partial(ut.session_set, 'role')), # method to set role into the session ('get_uid', lambda:ut.session_get('uid')), # method to get uid from the session ('get_role', lambda:ut.session_get('role')), # method to get role from the session ('set_message', partial(ut.session_set,'message')), ] @classmethod def config(cls, **kw): '''setting static methods''' for k,v in cls.CLS_ATTR: setattr(cls, k, staticmethod(kw.get(k,v))) @classmethod def login(cls, uid, pw): role=cls.get_role_from_db(uid, pw) if role: cls.set_uid(uid) cls.set_role(role) return role def __init__(self, appUrl, **kw): '''kw parameters are role, message and failure_redirect''' for k,v in [('message', 'Login required'), ('failure_redirect', appUrl + '/login')]: setattr(self, k, kw.get(k, v)) self.is_auth=(lambda :kw['role']==self.get_role()) if 'role' in kw else self.get_role def __call__(self, fun): '''acting as a decorator''' @wraps(fun) def _f(*a, **k): if self.is_auth(): return fun(*a, **k) else: self.set_message(self.message) return btl.redirect(self.failure_redirect) return _f
def add_user_form(): return template('add_user', appUrl=appUrl, message=ut.session_get('message', True), role=Auth.get_role())
def users(): return template('users', users=ut.users(), appUrl=appUrl, message=ut.session_get('message', True), role=Auth.get_role())
def login_form(): return {'message': ut.session_get('message', True)}