#!/usr/bin/env python3 # -*-coding:utf-8 -* import bottle import beaker.middleware import sauth app = bottle.app() session_opts = { 'session.type': 'file', 'session.data_dir': './sessions/', 'session.auto': True, } app = beaker.middleware.SessionMiddleware(app, session_opts) sa = sauth.SAuth() auth = sa.make_auth_decorator(fail_redirect="/login") def post_get(name, default=''): return bottle.request.POST.get(name, default).strip() def get_get(name, default=''): return bottle.request.GET.get(name, default).strip() @bottle.route('/') @bottle.view('home') def hello(): usn = sauth.get_username() usr = sauth.get_roles() if usn=='': usn=None return {'username':usn, 'roles':usr} @bottle.route('/login') @bottle.view('login') def login_form(): """Serve login form""" if get_get('error')=='': return {'error':False} else: return {'error':True} @bottle.post('/login') def login(): """Authenticate users""" username = post_get('username') password = post_get('password') sa.login(username, password, success_redirect='/private', fail_redirect='/login?error=1') @bottle.route('/logout') def logout(): sa.logout() return "LOGOUT DONE Back to home" @bottle.route('/password') @bottle.view('password') @auth() def password_form(): return {'username':sauth.get_username()} @bottle.post('/password') @auth() def password(): username = sauth.get_username() password = post_get('password') sa.set_password(username, password) bottle.redirect('/') @bottle.route('/private') @auth() def private(): return "Page for Log-in users only. Back to home" ################################################################ # Admin section ################################################################ @bottle.route('/admin') @bottle.view('admin') @auth(role='admin') def admin(): sa.refresh() luser = [] for u in sa.get_all_usernames(): us = sa.get_user(u) luser.append([u, us]) return {'user':sauth.get_username(), 'luser':luser, 'lroles':sa.get_all_roles()} @bottle.post('/admin/user/add') @auth(role='admin') def admin_user_add(): username = post_get('username') password = post_get('password') sa.add_user(username, password) bottle.redirect('/admin') return @bottle.route('/admin/user//del_role/') @auth(role='admin') def admin_user_delrole(user, role): sa.rm_user_role(user, role) bottle.redirect('/admin') return @bottle.post('/admin/user//add_role') @auth(role='admin') def admin_user_addrole(user): role = post_get('role') if not role=="": sa.add_user_role(user, role) bottle.redirect('/admin') return @bottle.post('/admin/user//password') @auth(role='admin') def admin_user_password(user): password = post_get('password') sa.set_password(user, password) bottle.redirect('/admin') return @bottle.route('/admin/user//rm') @auth(role='admin') def admin_user_rm(user): sa.rm_user(user) bottle.redirect('/admin') return @bottle.post('/admin/role/add') @auth(role='admin') def admin_role_add(): role = post_get('role') sa.add_role(role) bottle.redirect('/admin') return @bottle.route('/admin/role//del_sub/') @auth(role='admin') def admin_role_delsub(role, subrole): sa.rm_role(role, subrole=subrole) bottle.redirect('/admin') return @bottle.post('/admin/role//add_sub/') @auth(role='admin') def admin_role_addsub(role): subrole = post_get('addrole') print("1::subrole::{}".format(subrole)) if not subrole=='': sa.add_role(role, subrole=subrole) bottle.redirect('/admin') return @bottle.route('/admin/role//rm/') @auth(role='admin') def admin_role_rm(role): sa.rm_role(role) bottle.redirect('/admin') return bottle.run(app, host='localhost', port=8080)