Browse Source

re add attic

pull/10/head
Cian Butler 4 years ago
parent
commit
7053f5a765
8 changed files with 337 additions and 0 deletions
  1. +45
    -0
      attic/dump_userdb.sh
  2. +106
    -0
      attic/rebuild_userdb_staff.py
  3. +91
    -0
      attic/rebuild_userdb_students.py
  4. +10
    -0
      attic/userdb_reserved.sql
  5. +9
    -0
      attic/userdb_staff.sql
  6. +11
    -0
      attic/userdb_students.sql
  7. +44
    -0
      attic/userdb_users.sql
  8. +21
    -0
      attic/userdb_usertypes.sql

+ 45
- 0
attic/dump_userdb.sh View File

@@ -0,0 +1,45 @@
#!/bin/sh
#
# Dump databases to committee folder.
#
# $Id: update_userdb_dumps.sh,v 1.4 2002/02/16 21:50:54 cns Exp cns $
#

OUT=/local/committee/userdb
PSQL="/usr/local/pgsql/bin/psql -h localhost -d userdb"
PG_DUMP="/usr/local/pgsql/bin/pg_dump -h localhost -d userdb"
USERDB_BACKUP="/local/admin/scripts/users/backup-of-userdb.dump"

printf "$OUT dumps: "

# Dump each database and keep a copy of yesterday's database
# (for doing comparisons/diffs).
#
for i in users students reserved; do
$PSQL -c "select * from $i" > $OUT/$i.new
# Only rotate them if there was no error from psql
# and a non-empty dump was generated.
#
if [ $? -eq 0 -a -s $OUT/$i.new ]; then
printf "$i "
mv $OUT/$i $OUT/$i.yesterday
mv $OUT/$i.new $OUT/$i
else
printf "$i (FAILED) "
rm -f $OUT/$i.new
fi
done

echo "Done."

# Make sure committee can read 'em.
#
chmod 640 $OUT/*
chgrp committe $OUT/*

printf "Full dump: "

$PG_DUMP > $USERDB_BACKUP

echo "Done."

+ 106
- 0
attic/rebuild_userdb_staff.py View File

@@ -0,0 +1,106 @@
#! /usr/bin/env python

"""Rebuild staff table in userdb from DCU's LDAP staff tree."""

import ldap
import pgdb
import re

__version__ = "$Revision$"
__author__ = "Cillian Sharkey"

def main():
"""Program entry function."""

ldaphost = 'atlas.dcu.ie'
dbh = pgdb.connect(database = 'userdb')
cur = dbh.cursor()
l = ldap.open(ldaphost)
l.simple_bind_s('', '') # anonymous bind
print 'userdb/staff:',
print 'Search',
staff = l.search_s('ou=staff,o=dcu', ldap.SCOPE_SUBTREE, 'objectclass=person', ('fullName', 'givenName', 'sn', 'mail', 'cn', 'gecos'))
print '[%d].' % len(staff),
# Delete all existing entries.
#
print 'Purge.',
cur.execute('delete from staff')
# Add new entries.
#
print 'Populate.',
total = 0
ids = {}
insert_staff = "INSERT INTO staff (id, name, email) VALUES (%s, %s, %s)"
re_gecos = re.compile(r'^(.*),.*(\d{8})')
for i in staff:
try:
attr = i[1]
id = name = None
# Check gecos for full name and staff id.
#
if attr.has_key('gecos'):
res = re_gecos.search(attr['gecos'][0])
if res:
name = res.group(1)
id = int(res.group(2))
# If no id in gecos, cycle through each cn attribute value
# until we find one that is a number (which can only be the id
# number).
#
if not id:
if attr.has_key('cn'):
for j in attr['cn']:
try:
id = int(j)
except ValueError:
pass
else:
break
else:
# No id found!
continue
# Ignore entries with duplicate IDs.
#
if ids.has_key(id):
continue
else:
ids[id] = 1
# If no name found from gecos and no fullName attribute,
# construct their full name from first name ('givenName')
# followed by their surname ('sn').
#
if not name:
if attr.has_key('fullName'):
name = attr['fullName'][0]
else:
name = '%s %s' % (attr['givenName'][0], attr['sn'][0])
email = attr['mail'][0]
cur.execute(insert_staff, (id, name, email))
total += 1
except KeyError, e:
pass
print 'Done [%d/%d].' % (total, len(staff))

dbh.commit()
cur.execute('END; VACUUM ANALYZE staff')
dbh.close()
l.unbind()
if __name__ == "__main__":
main()

+ 91
- 0
attic/rebuild_userdb_students.py View File

@@ -0,0 +1,91 @@
#! /usr/bin/env python

"""Rebuild students table in userdb from DCU's LDAP student tree."""

import ldap
import pgdb

__version__ = "$Revision: 1.1 $"
__author__ = "Cillian Sharkey"

def main():
"""Program entry function."""

ldaphost = 'atlas.dcu.ie'
dbh = pgdb.connect(database = 'userdb')
cur = dbh.cursor()
l = ldap.open(ldaphost)
l.simple_bind_s('', '') # anonymous bind
print 'userdb/students:',
print 'Search',
students = l.search_s('ou=students,o=dcu', ldap.SCOPE_SUBTREE, 'objectclass=person', ('givenName', 'sn', 'mail', 'l', 'cn'))
print '[%d].' % len(students),
# Delete all existing entries.
#
print 'Purge.',
cur.execute('delete from students')
# Add new entries.
#
print 'Populate.',
total = 0
ids = {}
insert_student = 'INSERT INTO students (id, name, email, course, year) VALUES (%d, %s, %s, %s, %s)'
for i in students:
try:
attr = i[1]
# Extract course & year from 'l' attribute value. Assumes last
# character is the year (1, 2, 3, 4, X, O, C, etc.) and the rest is the
# course name. Uppercase course & year for consistency.
#
course = attr['l'][0][:-1].upper() or 'N/A'
year = attr['l'][0][-1].upper() or 'N/A'
# Cycle through each 'cn' attribute value until we find one that is a
# number (which can only be the id number).
#
for j in attr['cn']:
try:
id = int(j)
except ValueError:
pass
else:
break
else:
# No ID number found! Skip this ldap entry.
continue
if ids.has_key(id):
continue
else:
ids[id] = 1
# Construct their full name from first name ('givenName') followed by
# their surname ('sn').
#
name = '%s %s' % (attr['givenName'][0], attr['sn'][0])
email = attr['mail'][0]
cur.execute(insert_student, (id, name, email, course, year))
total += 1
except KeyError:
pass
print 'Done [%d/%d].' % (total, len(students))
dbh.commit()
cur.execute('END; VACUUM ANALYZE students')
dbh.close()
l.unbind()


if __name__ == "__main__":
main()

+ 10
- 0
attic/userdb_reserved.sql View File

@@ -0,0 +1,10 @@
-- $Id$

CREATE TABLE "reserved" (
username VARCHAR(8) NOT NULL PRIMARY KEY,
info VARCHAR NOT NULL
);

GRANT ALL ON reserved TO root;
GRANT SELECT ON users TO webgroup;
GRANT SELECT ON users TO www;

+ 9
- 0
attic/userdb_staff.sql View File

@@ -0,0 +1,9 @@
-- $Id$

CREATE TABLE "staff" (
id INT PRIMARY KEY,
name VARCHAR NOT NULL,
email VARCHAR NOT NULL
);

GRANT ALL ON staff TO root;

+ 11
- 0
attic/userdb_students.sql View File

@@ -0,0 +1,11 @@
-- $Id$

CREATE TABLE "students" (
id INT PRIMARY KEY,
name VARCHAR NOT NULL,
email VARCHAR NOT NULL,
course VARCHAR NOT NULL,
year VARCHAR NOT NULL
);

GRANT ALL ON students TO root;

+ 44
- 0
attic/userdb_users.sql View File

@@ -0,0 +1,44 @@
-- $Id$

CREATE TABLE "users" (
username VARCHAR(8) PRIMARY KEY,
usertype VARCHAR NOT NULL CONSTRAINT valid_usertype REFERENCES usertypes,
name VARCHAR NOT NULL CONSTRAINT require_name CHECK (name != ''),
newbie BOOLEAN NOT NULL,
email VARCHAR NOT NULL CONSTRAINT require_email CHECK (email != ''),

id INT UNIQUE CONSTRAINT require_id CHECK ((usertype != 'member' AND usertype != 'associat' AND usertype != 'staff') OR (id IS NOT NULL AND id > 0)),
course VARCHAR CONSTRAINT require_course CHECK (usertype != 'member' OR (course IS NOT NULL AND course != '')),
year VARCHAR CONSTRAINT require_year CHECK (usertype != 'member' OR year IS NOT NULL),
years_paid INT CONSTRAINT require_years_paid CHECK ((usertype != 'member' AND usertype != 'associat' AND usertype != 'staff') OR (years_paid IS NOT NULL)),

created_by VARCHAR(8) NOT NULL CONSTRAINT require_created_by CHECK (created_by != ''),
created_at TIMESTAMP(0) NOT NULL DEFAULT now(),
updated_by VARCHAR(8) NOT NULL CONSTRAINT require_updated_by CHECK (updated_by != ''),
updated_at TIMESTAMP(0) NOT NULL DEFAULT now(),

-- Following are optional or non-essential additions
birthday DATE
);

GRANT ALL ON users TO root;
GRANT SELECT ON users TO webgroup;
GRANT SELECT ON users TO www;

/*
* Set updated_at to current time for each update
*/

CREATE FUNCTION users_updated_at_stamp () RETURNS OPAQUE AS '
DECLARE
curtime timestamp;
BEGIN
curtime = ''now'';
NEW.updated_at := curtime;
RETURN NEW;
END;
' LANGUAGE 'plpgsql';
CREATE TRIGGER users_updated_at BEFORE UPDATE ON users
FOR EACH ROW EXECUTE PROCEDURE users_updated_at_stamp();


+ 21
- 0
attic/userdb_usertypes.sql View File

@@ -0,0 +1,21 @@
-- $Id$

CREATE TABLE "usertypes" (
usertype VARCHAR(8) PRIMARY KEY
);

GRANT ALL ON usertypes TO root;

INSERT INTO "usertypes" VALUES ('reserved');
INSERT INTO "usertypes" VALUES ('system');
INSERT INTO "usertypes" VALUES ('founders');
INSERT INTO "usertypes" VALUES ('member');
INSERT INTO "usertypes" VALUES ('associat');
INSERT INTO "usertypes" VALUES ('club');
INSERT INTO "usertypes" VALUES ('society');
INSERT INTO "usertypes" VALUES ('intersoc');
INSERT INTO "usertypes" VALUES ('projects');
INSERT INTO "usertypes" VALUES ('redbrick');
INSERT INTO "usertypes" VALUES ('guest');
INSERT INTO "usertypes" VALUES ('staff');
INSERT INTO "usertypes" VALUES ('dcu');

Loading…
Cancel
Save