~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/userservice/__init__.py

Direct port of userservice to the new framework.

Show diffs side-by-side

added added

removed removed

Lines of Context:
154
154
from ivle.auth import AuthError, authenticate
155
155
import urllib
156
156
 
 
157
from ivle.webapp.base.views import BaseView
 
158
from ivle.webapp.base.plugins import ViewPlugin
 
159
from ivle.webapp.errors import NotFound
 
160
 
157
161
# The user must send this declaration message to ensure they acknowledge the
158
162
# TOS
159
163
USER_DECLARATION = "I accept the IVLE Terms of Service"
166
170
    "svn_pass"
167
171
)
168
172
 
169
 
def handle(req):
170
 
    """Handler for the Console Service AJAX backend application."""
171
 
    if req.user is None and req.path != 'activate_me':
172
 
        # Not logged in.
 
173
class UserServiceView(BaseView):
 
174
    def __init__(self, req, path):
 
175
        if len(path) > 0 and path[-1] == os.sep:
 
176
            self.path = path[:-1]
 
177
        else:
 
178
            self.path = path
 
179
 
 
180
    def authorize(self, req):
173
181
        # XXX: activate_me isn't called by a valid user, so is special for now.
174
 
        req.throw_error(req.HTTP_FORBIDDEN,
175
 
        "You are not logged in to IVLE.")
176
 
    if len(req.path) > 0 and req.path[-1] == os.sep:
177
 
        path = req.path[:-1]
178
 
    else:
179
 
        path = req.path
180
 
    # The path determines which "command" we are receiving
181
 
    fields = req.get_fieldstorage()
182
 
    try:
183
 
        func = actions_map[req.path]
184
 
    except KeyError:
185
 
        req.throw_error(req.HTTP_BAD_REQUEST,
186
 
        "%s is not a valid userservice action." % repr(req.path))
187
 
    func(req, fields)
 
182
        if req.path == 'activate_me' and get_user_details(req) is not None:
 
183
            return True
 
184
        return req.user is not None
 
185
 
 
186
    def render(self, req):
 
187
        # The path determines which "command" we are receiving
 
188
        fields = req.get_fieldstorage()
 
189
        try:
 
190
            func = actions_map[self.path]
 
191
        except KeyError:
 
192
            raise NotFound()
 
193
        func(req, fields)
 
194
 
 
195
class Plugin(ViewPlugin):
 
196
    urls = [
 
197
        ('userservice/*path', UserServiceView)
 
198
    ]
188
199
 
189
200
@require_method('POST')
190
201
def handle_activate_me(req, fields):
206
217
    their acceptance). It must only be called through a POST request.
207
218
    """
208
219
 
209
 
    # XXX: Very special because we don't have a valid user, so req.user is
210
 
    # None.
211
 
    user = get_user_details(req)
212
 
 
213
 
    if user is None:
214
 
        req.throw_error(req.HTTP_FORBIDDEN,
215
 
        "You are not logged in to IVLE.")
216
 
 
217
220
    try:
218
221
        try:
219
222
            declaration = fields.getfirst('declaration')