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

« back to all changes in this revision

Viewing changes to lib/common/makeuser.py

  • Committer: mattgiuca
  • Date: 2008-02-05 01:41:15 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:409
Moved www/conf and www/common to a new directory lib. This separates the "web"
part of IVLE from what is becoming less web oriented (at least from Apache's
standpoint).
Modified setup.py to install this lib directory correctly and write conf in
the right place. Also adds the lib directory to ivle.pth.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
# TODO: When creating a new home directory, chown it to its owner
33
33
 
34
 
import md5
35
34
import os
36
 
import stat
37
35
import shutil
38
 
import time
39
 
import uuid
40
36
import warnings
41
37
 
42
38
import conf
43
39
import db
44
40
 
45
 
def make_svn_repo(login, throw_on_error=True):
46
 
    """Create a repository for the given user.
47
 
    """
48
 
    path = os.path.join(conf.svn_repo_path, login)
49
 
    try:
50
 
        res = os.system("svnadmin create '%s'" % path)
51
 
        if res != 0 and throw_on_error:
52
 
            raise Exception("Cannot create repository for %s" % login)
53
 
    except Exception, exc:
54
 
        print repr(exc)
55
 
        if throw_on_error:
56
 
            raise
57
 
    try:
58
 
        os.system("chown -R www-data:www-data %s" % path)
59
 
    except Exception:
60
 
        pass
61
 
 
62
 
def rebuild_svn_config():
63
 
    """Build the complete SVN configuration file.
64
 
    """
65
 
    conn = db.DB()
66
 
    res = conn.query("SELECT login, rolenm FROM login;").dictresult()
67
 
    groups = {}
68
 
    for r in res:
69
 
        role = r['rolenm']
70
 
        if role not in groups:
71
 
            groups[role] = []
72
 
        groups[role].append(r['login'])
73
 
    f = open(conf.svn_conf + ".new", "w")
74
 
    f.write("# IVLE SVN Repositories Configuration\n")
75
 
    f.write("# Auto-generated on %s\n" % time.asctime())
76
 
    f.write("\n")
77
 
    f.write("[groups]\n")
78
 
    for (g,ls) in groups.iteritems():
79
 
        f.write("%s = %s\n" % (g, ",".join(ls)))
80
 
    f.write("\n")
81
 
    for r in res:
82
 
        login = r['login']
83
 
        f.write("[%s:/]\n" % login)
84
 
        f.write("%s = rw\n" % login)
85
 
        #f.write("@tutor = r\n")
86
 
        #f.write("@lecturer = rw\n")
87
 
        #f.write("@admin = rw\n")
88
 
        f.write("\n")
89
 
    f.close()
90
 
    os.rename(conf.svn_conf + ".new", conf.svn_conf)
91
 
 
92
 
def make_svn_config(login, throw_on_error=True):
93
 
    """Add an entry to the apache-svn config file for the given user.
94
 
       Assumes the given user is either a guest or a student.
95
 
    """
96
 
    f = open(conf.svn_conf, "a")
97
 
    f.write("[%s:/]\n" % login)
98
 
    f.write("%s = rw\n" % login)
99
 
    #f.write("@tutor = r\n")
100
 
    #f.write("@lecturer = rw\n")
101
 
    #f.write("@admin = rw\n")
102
 
    f.write("\n")
103
 
    f.close()
104
 
 
105
 
def make_svn_auth(login, throw_on_error=True):
106
 
    """Setup svn authentication for the given user.
107
 
       FIXME: create local.auth entry
108
 
    """
109
 
    passwd = md5.new(uuid.uuid4().bytes).digest().encode('hex')
110
 
    if os.path.exists(conf.svn_auth_ivle):
111
 
        create = ""
112
 
    else:
113
 
        create = "c"
114
 
 
115
 
    db.DB().update_user(login, svn_pass=passwd)
116
 
 
117
 
    res = os.system("htpasswd -%smb %s %s %s" % (create,
118
 
                                              conf.svn_auth_ivle,
119
 
                                              login, passwd))
120
 
    if res != 0 and throw_on_error:
121
 
        raise Exception("Unable to create ivle-auth for %s" % login)
122
 
 
123
 
    return passwd
124
 
 
125
 
def make_jail(username, uid, force=True):
 
41
def makeuser(username, password, nick, fullname, rolenm, studentid):
 
42
    """Creates a new user on a pre-installed system.
 
43
    Sets up the following:
 
44
    * User's jail and home directory within the jail.
 
45
    * Subversion repository
 
46
    * Check out Subversion workspace into jail
 
47
    * Database details for user
 
48
    * Unix user account
 
49
    """
 
50
    homedir = make_jail(username)
 
51
    make_user_db(username, password, nick, fullname, rolenm, studentid)
 
52
    # TODO: -p password (need to use crypt)
 
53
    if os.system("useradd -d %s '%s'" % (homedir, username)) != 0:
 
54
        raise Exception("Failed to add Unix user account")
 
55
 
 
56
def make_jail(username, force=True):
126
57
    """Creates a new user's jail space, in the jail directory as configured in
127
58
    conf.py.
128
59
 
133
64
 
134
65
    Returns the path to the user's home directory.
135
66
 
136
 
    Chowns the user's directory within the jail to the given UID.
137
 
 
138
 
    Note: This takes separate username and uid arguments. The UID need not
139
 
    *necessarily* correspond to a Unix username at all, if all you are
140
 
    planning to do is setuid to it. This allows the caller the freedom of
141
 
    deciding the binding between username and uid, if any.
142
 
 
143
67
    force: If false, exception if jail already exists for this user.
144
68
    If true (default), overwrites it, but preserves home directory.
145
69
    """
203
127
        # Set up the user's home directory
204
128
        userhomedir = os.path.join(homedir, username)
205
129
        os.mkdir(userhomedir)
206
 
        # Chown (and set the GID to the same as the UID).
207
 
        os.chown(userhomedir, uid, uid)
208
 
        # Chmod to rwxr-xr-x (755)
209
 
        os.chmod(userhomedir, 0755)
210
130
        return userhomedir
211
131
 
212
132
def linktree(src, dst):
248
168
    if errors:
249
169
        raise Exception, errors
250
170
 
251
 
def make_user_db(throw_on_error = True, **kwargs):
 
171
def make_user_db(login, password, nick, fullname, rolenm, studentid,
 
172
    force=True):
252
173
    """Creates a user's entry in the database, filling in all the fields.
253
 
    All arguments must be keyword args. They are the fields in the table.
254
 
    However, instead of supplying a "passhash", you must supply a
255
 
    "password" argument, which will be hashed internally.
256
 
    Also do not supply a state. All users are created in the "no_agreement"
257
 
    state.
258
 
    Throws an exception if the user already exists.
 
174
    If force is False, throws an exception if the user already exists.
 
175
    If True, overwrites the user's entry in the DB.
259
176
    """
260
177
    dbconn = db.DB()
261
 
    dbconn.create_user(**kwargs)
 
178
    if force:
 
179
        # Delete user if it exists
 
180
        try:
 
181
            dbconn.delete_user(login)
 
182
        except:
 
183
            pass
 
184
    dbconn.create_user(login, password, nick, fullname, rolenm, studentid)
262
185
    dbconn.close()
263
 
 
264
 
    if kwargs['password']:
265
 
        if os.path.exists(conf.svn_auth_local):
266
 
            create = ""
267
 
        else:
268
 
            create = "c"
269
 
        res = os.system("htpasswd -%smb %s %s %s" % (create,
270
 
                                                     conf.svn_auth_local,
271
 
                                                     kwargs['login'],
272
 
                                                     kwargs['password']))
273
 
        if res != 0 and throw_on_error:
274
 
            raise Exception("Unable to create local-auth for %s" % kwargs['login'])
275