class Root: _cp_config = {'tools.sessions.on': True, 'tools.auth.on': True} auth = AuthController() restricted = RestrictedArea() @cherrypy.expose @require() def index(self): return """This page only requires a valid login.""" @cherrypy.expose def open(self): return """This page is open to everyone""" @cherrypy.expose @require(name_is("joe")) def only_for_joe(self): return """Hello Joe - this page is available to you only""" # This is only available if the user name is joe _and_ he's in group admin @cherrypy.expose @require(name_is("joe")) @require(member_of("admin") ) # equivalent: @require(name_is("joe"), member_of("admin")) def only_for_joe_admin(self): return """Hello Joe Admin - this page is available to you only"""
class Root: _cp_config = { 'tools.sessions.on': True, 'tools.auth.on': True } auth = AuthController() restricted = RestrictedArea() def generatelogoutbutton(self): return """<form method="post" action="/auth/logout"><input type="submit" value="log out" />""" @cherrypy.expose @require() def generate(self): return open("generate.html") @cherrypy.expose @require() def index(self): s = "<html><body>This page only requires a valid login. You are logged in as: " + cherrypy.request.login s += self.generatelogoutbutton() s += "<br><br>" s += "<a href=/auth/logout>Logout</a><br>" if cherrypy.request.login == 'joe': s += "<a href=/only_for_joe>Only For Joe</a><br>" if member_of("admin"): s += "<a href=/only_for_joe_admin>Only For Joe Admin</a><br>" s += "<a href=/generate>Generate Random String</a><br>" s += "<a href=/open>Open page</a><br>" s += "</body></html>" return s @cherrypy.expose def open(self): s = "This page is open to everyone. " return s @cherrypy.expose @require(name_is("joe")) def only_for_joe(self): return """Hello Joe - this page is available to you only""" # This is only available if the user name is joe _and_ he's in group admin @cherrypy.expose @require(name_is("joe")) @require(member_of("admin")) # equivalent: @require(name_is("joe"), member_of("admin")) def only_for_joe_admin(self): return """Hello Joe Admin - this page is available to you only"""
class Root(object): auth = AuthController() restricted = RestrictedArea() object = Any_object() employee = Employee() timeline = Timeline() clients = Clients() ktree = KTree() account = Account() cases = Case() @cherrypy.expose @require(member_of("users")) def index(self): tmpl = lookup.get_template("dashboard.html") c = get_session_context(cherrypy.request.login) params = cherrypy.request.headers rwQueue.msg_delivery_for_user.delay(str(c['uuid'])) G.reload() return tmpl.render(params=params, session_context=c, G=G) @cherrypy.expose @require(member_of("admin")) def autoclassify_all_notlinked_objects(self): try: rwLearn.autoclassify_all_notlinked_objects() except Exception as e: return ShowError("Обновить автоклассификацию нельзя. " + str(e)) else: pass raise cherrypy.HTTPRedirect("/settings?menu=ktree") @cherrypy.expose @require(member_of("users")) def settings(self, menu=None): session_context = cherrypy.session.get('session_context') if not menu or menu == "": tmpl = lookup.get_template("settings_dashboard.html") session_context['back_ref'] = '/' session_context['menu'] = "settings" params = cherrypy.request.headers cherrypy.session['session_context'] = session_context return tmpl.render(params=params, session_context=session_context) elif menu == 'company': tmpl = lookup.get_template("settings_dashboard.html") session_context['back_ref'] = '/settings' session_context['menu'] = "company" elif menu == 'employee' or menu == 'accounts': tmpl = lookup.get_template("employee.html") session = rwObjects.Session() # если пользователь с правами администратора, выбираем всех сотрудников if 'admin' in session_context['groups']: users = session.query(rwObjects.Employee). \ filter_by(comp_id=session_context['comp_id']).all() obj_keys = users[0].get_attrs() f = users[0].get_fields() session_context['back_ref'] = '/settings' session_context['menu'] = "employee" # если пользователь с обычными правами, только свой профиль else: users = [rwObjects.get_by_uuid(session_context['uuid'])[0]] obj_keys = users[0].get_attrs() f = users[0].get_fields() session_context['back_ref'] = '/settings' session_context['menu'] = "accounts" linked_objects = dict() for user in users: refs = session.query(rwObjects.Reference). \ filter(rwObjects.sqlalchemy.and_(rwObjects.Reference.source_uuid == user.uuid, rwObjects.Reference.target_type == "accounts", rwObjects.Reference.link == 0)).all() linked_objects[user.uuid] = [] for ref in refs: linked_objects[user.uuid].append(rwObjects.get_by_uuid(ref.target_uuid)[0]) session.close() cherrypy.session['session_context'] = session_context return tmpl.render(obj=users, keys=obj_keys, session_context=session_context, view_f=f[1], all_f=f[0], linked=linked_objects) elif menu == 'clients': tmpl = lookup.get_template("settings_dashboard.html") session_context['back_ref'] = '/settings?menu=clients' elif menu == 'ktree': tmpl = lookup.get_template("ktree_settings.html") session = rwObjects.Session() tree = rwObjects.KnowledgeTree() session_context['back_ref'] = '/settings?menu=ktree' session_context['menu'] = "ktree_settings" return tmpl.render(obj=tree, session=session, session_context=session_context) else: print "меню без указания." tmpl = lookup.get_template("settings_dashboard.html") session_context['back_ref'] = '/settings' session_context['menu'] = "settings" params = cherrypy.request.headers return tmpl.render(params=params, session_context=session_context) @cherrypy.expose @require(member_of("users")) def help(self, menu=None): tmpl = lookup.get_template("help.html") session_context = cherrypy.session.get('session_context') return tmpl.render(session_context=session_context) @cherrypy.expose def open(self): return """This page is open to everyone""" @cherrypy.expose @require(name_is("joe")) def only_for_joe(self): return """Hello Joe - this page is available to you only""" # This is only available if the user name is joe _and_ he's in group admin @cherrypy.expose @require(name_is("joe")) @require(member_of("admin")) # equivalent: @require(name_is("joe"), member_of("admin")) def only_for_joe_admin(self): return """Hello Joe Admin - this page is available to you only"""
class Root: _cp_config = {'tools.sessions.on': True, 'tools.auth.on': True} auth = AuthController() restricted = RestrictedArea() @cherrypy.expose @require() def index(self): return """ This page only requires a valid login. <a href="auth/logout">LOUGOUT</a> """ def get_postform(self, msg="post", from_page="/", username=None): wall1 = wall.wall(username) wall1.body = \ """<form method="post" action="/post"> <input type="hidden" name="from_page" value="%(from_page)s" /> %(msg)s<br /> Post: <input type="text" name="message" /> <br /> <input type="submit" value="Post" /> """ %locals() wall1.pull(username) return wall1.return_html() def post_to_database(self, message, username): con = mdb.connect('localhost', 'testuser', 'test623', 'testdb') with con: cur = con.cursor() query = "INSERT INTO Message(Username, Post) VALUES('%(username)s', '%(message)s')" % locals( ) cur.execute(query) return None return u"database error" @cherrypy.expose @require() def post(self, message=None, from_page="/"): username = cherrypy.session.get(SESSION_KEY) if message == None: return self.get_postform(from_page=from_page, username=username) error_msg = self.post_to_database(message, username) if error_msg: return self.get_postform(self, error_msg, from_page, username) else: raise cherrypy.HTTPRedirect("/post") @cherrypy.expose def gethint(self, q): print "hello" print q return "no suggestion" @cherrypy.expose def open(self): home_page = html_page.html_page("html_src/showhint") return home_page.get_html() @cherrypy.expose @require(name_is("joe")) def only_for_joe(self): return """Hello Joe - this page is available to you only""" # This is only available if the user name is joe _and_ he's in group admin @cherrypy.expose @require(name_is("joe")) @require(member_of("admin") ) # equivalent: @require(name_is("joe"), member_of("admin")) def only_for_joe_admin(self): return """Hello Joe Admin - this page is available to you only"""