source: cows_wps/trunk/cows_wps/controllers/admin.py @ 7118

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/controllers/admin.py@7118
Revision 7118, 3.5 KB checked in by astephen, 10 years ago (diff)

more fixes for admin interface

Line 
1import logging
2
3from pylons import request, response, session, tmpl_context as c
4from pylons.controllers.util import abort, redirect_to
5from routes import url_for
6
7from cows_wps.model.managers import requestManager
8from cows_wps.model.orm import tables
9
10from cows_wps.renderer.ui_renderer import *
11from cows_wps.controllers import *
12from cows_wps.lib.ui.proc_config import *
13from cows_wps.utils.common import *
14
15log = logging.getLogger(__name__)
16
17
18class AdminController(BaseController):
19
20    def index(self):
21        # Check cookie first
22        if isAdminLoggedIn(request):
23            return self._successfulLogin() 
24     
25        sess = requestManager.getSession()
26        res = sess.query(tables.admin)[0]
27        admin_u = str(res.admin_name)
28        admin_p = str(res.admin_password)
29
30        admin_name = None
31        admin_password = None
32 
33        if "admin_name" in request.params.keys():
34            admin_name = str(request.params.getone("admin_name"))
35        if "admin_password" in request.params.keys():
36            admin_password = str(request.params.getone("admin_password"))
37
38        err_msg = ""
39
40        if admin_name != None:
41            if admin_name != admin_u or admin_password == None: 
42                err_msg = "Incorrect login details provided. Please try again."
43                return self.login(err_msg = err_msg)
44            else:
45                if admin_password != admin_p:
46                    err_msg = "Incorrect login details provided. Please try again."
47                    return self.login(err_msg = err_msg)
48                else:
49                    return self._successfulLogin()
50        else:
51            return self.login()
52       
53
54    def _successfulLogin(self):
55        response.set_cookie('wps_ui_admin', request.environ["REMOTE_ADDR"], expires = 3600)
56        html = """      <p>You are logged in as the administrator.</p>
57        <p>Please try using the <a href="%s">jobs page</a> to view and manage everyone's jobs.</p>
58        <p>Click <a href="%s">logout</a> to log out.</p>""" % (url_for(controller = 'jobs'), url_for(controller = 'admin', action = 'logout'))
59        renderer = UIPageRenderer()
60        resp = renderer.render("Administrator page",
61                               [("Administrator", html)],
62                               admin = True)
63        return resp
64
65
66    def login(self, err_msg = ""):
67        """
68        Show admin login page.
69        """
70        login_form = ""
71
72        if err_msg != "":
73            login_form = """<center><div id="bad_login" style="width: 600px; border-width: 0px; background: orange; color: white; font-weight: bold; padding: 3px;">%s</div></center>""" % err_msg
74
75        login_form += """<form action="/admin" onSubmit="return validateTextInput(document.getElementById('admin_name').value, /^[a-zz0-9A-Z]{4,20}$/, 'The user name must be between 4 and 20 letters and/or numbers.');" method="POST">
76                <p>Please enter the WPS UI Administrator account details below.</p>
77                <label class="text_width_150"><b>Username: </b></label>
78                <input type="text" name="admin_name" value="" size="20" /><br />
79                <label class="text_width_150"><b>Password: </b></label>
80                <input type="password" name="admin_password" value="" size="20" />&nbsp;&nbsp;
81                <input type="submit" value="Login" />
82        </form>
83        """
84        renderer = UIPageRenderer()
85        resp = renderer.render("Administrator page", 
86                               [("Login", login_form)])
87        return resp
88
89       
90    def logout(self):
91        response.delete_cookie('wps_ui_admin')
92        return self.login(err_msg = "You have successfully logged out.")
Note: See TracBrowser for help on using the repository browser.