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

« back to all changes in this revision

Viewing changes to ivle/studpath.py

  • Committer: William Grant
  • Date: 2009-05-27 06:49:25 UTC
  • Revision ID: grantw@unimelb.edu.au-20090527064925-avjig8opo22t1lur
Drop ivle.conf usage from ivle.pulldown_subj.

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
 
 
116
    urlpath = os.path.normpath(urlpath)
 
117
    # If it begins with '..', it's illegal.
 
118
    if urlpath.startswith(".."):
 
119
        return None
 
120
 
 
121
    return os.path.join('/home', urlpath)
 
122
 
104
123
def svnpublished(path):
105
124
    """Given a path on the LOCAL file system, determines whether the path has
106
125
    its "ivle:published" property active (in subversion). Returns True
132
151
        return False
133
152
 
134
153
 
135
 
def authorize(req):
 
154
def authorize(req, user):
136
155
    """Given a request, checks whether req.user is allowed to
137
156
    access req.path. Returns None on authorization success. Raises
138
157
    HTTP_FORBIDDEN on failure.
145
164
    urlpath = os.path.normpath(req.path)
146
165
    # Now if it begins with ".." or separator, then it's illegal
147
166
    if urlpath.startswith("..") or urlpath.startswith(os.sep):
148
 
        req.throw_error(req.HTTP_FORBIDDEN)
 
167
        return False
149
168
 
150
169
    (owner, _) = util.split_path(urlpath)
151
 
    if req.user.login != owner:
152
 
        req.throw_error(req.HTTP_FORBIDDEN)
 
170
    if user.login != owner:
 
171
        return False
 
172
    return True
153
173
 
154
174
def authorize_public(req):
155
175
    """A different kind of authorization. Rather than making sure the
161
181
    Same interface as "authorize" - None on success, HTTP_FORBIDDEN exception
162
182
    raised on failure.
163
183
    """
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)
 
184
    _, path = url_to_local(req.config, req.path)
 
185
 
 
186
    # Walk up the tree, and find the deepest directory.
 
187
    while not os.path.isdir(path):
 
188
        path = os.path.dirname(path)
 
189
 
 
190
    if not (worldreadable(path) and published(path)):
 
191
        return False
 
192
    return True