admin.py

importbottlefrombeaker.middlewareimportSessionMiddlewarefromcorkimportCorkfromloginimport*@bottle.route('/admin')defadmin():"""Only administrators can see this"""aaa.require(role='admin',fail_redirect='/sorry_page')return'Welcome administrators'@bottle.route('/register',method='POST')defregister():"""Users can create new accounts, but only with 'user' role"""username=request.POST.get('user','')password=request.POST.get('pwd','')email_addr=request.POST.get('email_addr','')aaa.register(username,password,email_addr)return'Please check your inbox.'~

login.py

importbottlefrombeaker.middlewareimportSessionMiddlewarefromcorkimportCork# Setup Beaker middleware to handle sessions and cookiesapp=bottle.default_app()session_opts={'session.type':'cookie','session.validate_key':True,}app=SessionMiddleware(app,session_opts)# Use users.json and roles.json in the local example_conf directoryaaa=Cork('conf')@bottle.route('/login',method='POST')deflogin():username=bottle.request.POST.get('user','')password=bottle.request.POST.get('pwd','')# return username,passwordaaa.login(username,password,success_redirect='/',fail_redirect='/login')@bottle.route('/login')@bottle.view('login_form')deflogin_form():"""Serve login form"""return{}@bottle.route('/logout')deflogout():aaa.logout(success_redirect='/login')@bottle.route('/sorry_page')defsorry_page():"""Serve sorry page"""return'<p>Sorry, you are not authorized to perform this action</p>'