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

« back to all changes in this revision

Viewing changes to ivle/studpath.py

  • Committer: William Grant
  • Date: 2009-06-29 01:55:55 UTC
  • mto: This revision was merged to the branch mainline in revision 1322.
  • Revision ID: grantw@unimelb.edu.au-20090629015555-49xxv0piiieunx8e
Add docs on configuring Apache.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import stat
29
29
import pysvn
30
30
 
31
 
import ivle.conf
32
31
from ivle import util
33
32
 
34
33
# Make a Subversion client object (for published)
35
34
svnclient = pysvn.Client()
36
35
 
37
 
def url_to_local(urlpath):
 
36
def url_to_local(config, urlpath):
38
37
    """Given a URL path (part of a URL query string, see below), returns a
39
38
    tuple of
40
39
        * the username of the student whose directory is being browsed
51
50
 
52
51
    Returns (None, None) if the path is empty.
53
52
 
54
 
    See also: ivle.conf.jail_base
 
53
    >>> stubconfig = {'paths': {'jails': {'mounts': '/jails'}}}
 
54
    >>> url_to_local(stubconfig, '')
 
55
    (None, None)
 
56
    >>> url_to_local(stubconfig, 'joe/foo/bar/baz')
 
57
    ('joe', '/jails/joe/home/joe/foo/bar/baz')
55
58
    """
 
59
 
56
60
    # First normalise the path
57
61
    urlpath = os.path.normpath(urlpath)
58
62
    # Now if it begins with ".." or separator, then it's illegal
68
72
    # accordance with our directory scheme.
69
73
    # (The first time is the name of the jail, the second is the user's home
70
74
    # directory within the jail).
71
 
    path = os.path.join(ivle.conf.jail_base, user, 'home', urlpath)
 
75
    path = os.path.join(config['paths']['jails']['mounts'],
 
76
                        user, 'home', urlpath)
72
77
 
73
78
    return (user, path)
74
79
 
75
 
def url_to_jailpaths(urlpath):
 
80
def url_to_jailpaths(config, urlpath):
76
81
    """Given a URL path (part of a URL query string), returns a tuple of
77
82
        * the username of the student whose directory is being browsed
78
83
        * the absolute path where the jail will be located.
96
101
    (user, subpath) = util.split_path(urlpath)
97
102
    if user is None: return (None, None, None)
98
103
 
99
 
    jail = os.path.join(ivle.conf.jail_base, user)
100
 
    path = os.path.join('/home', urlpath)
 
104
    jail = os.path.join(config['paths']['jails']['mounts'], user)
 
105
    path = to_home_path(urlpath)
101
106
 
102
107
    return (user, jail, path)
103
108
 
 
109
def to_home_path(urlpath):
 
110
    """Given a URL path (eg. joe/foo/bar/baz), returns a path within the home.
 
111
 
 
112
    >>> to_home_path('joe/foo/bar/baz')
 
113
    '/home/joe/foo/bar/baz'
 
114
    """
 
115
    return os.path.join('/home', urlpath)
 
116
 
104
117
def svnpublished(path):
105
118
    """Given a path on the LOCAL file system, determines whether the path has
106
119
    its "ivle:published" property active (in subversion). Returns True
132
145
        return False
133
146
 
134
147
 
135
 
def authorize(req):
 
148
def authorize(req, user):
136
149
    """Given a request, checks whether req.user is allowed to
137
150
    access req.path. Returns None on authorization success. Raises
138
151
    HTTP_FORBIDDEN on failure.
145
158
    urlpath = os.path.normpath(req.path)
146
159
    # Now if it begins with ".." or separator, then it's illegal
147
160
    if urlpath.startswith("..") or urlpath.startswith(os.sep):
148
 
        req.throw_error(req.HTTP_FORBIDDEN)
 
161
        return False
149
162
 
150
163
    (owner, _) = util.split_path(urlpath)
151
 
    if req.user.login != owner:
152
 
        req.throw_error(req.HTTP_FORBIDDEN)
 
164
    if user.login != owner:
 
165
        return False
 
166
    return True
153
167
 
154
168
def authorize_public(req):
155
169
    """A different kind of authorization. Rather than making sure the
161
175
    Same interface as "authorize" - None on success, HTTP_FORBIDDEN exception
162
176
    raised on failure.
163
177
    """
164
 
    _, path = url_to_local(req.path)
165
 
    dirpath, _ = os.path.split(path)
166
 
    if not (worldreadable(dirpath) and published(dirpath)):
167
 
        req.throw_error(req.HTTP_FORBIDDEN)
 
178
    _, path = url_to_local(req.config, req.path)
 
179
 
 
180
    # Walk up the tree, and find the deepest directory.
 
181
    while not os.path.isdir(path):
 
182
        path = os.path.dirname(path)
 
183
 
 
184
    if not (worldreadable(path) and published(path)):
 
185
        return False
 
186
    return True