import web
import os
import sys
import re

NAME_RE = re.compile(r'[a-zA-Z_][-a-zA-Z0-9_.]+')

DEBUG=False

DATABASE_ACCESS=None
PLUGINDATA=None
COMMANDS=None

def myWebError(msg):
    return web.HTTPError(msg, {"Content-type": "text/plain"}, msg+'\n\n')

class UserPlugin:
    def __init__(self):
        pass
    
    def index(self, token=None):
        raise web.notfound()
    
    def add(self, token=None, name=None, password=None, returnTo=None):
        if not NAME_RE.match(name):
            raise myWebError('400 Illegal name for user')
        if DATABASE_ACCESS.get_key('identities/%s' % name, 'application/x-python-object', 'multi', token):
            raise myWebError('400 user already exists')
        # Create identities item
        DATABASE_ACCESS.put_key('identities/%s' % name, 'text/plain', 'ref', '', 'text/plain', token, replace=True)
        # Create people item
        DATABASE_ACCESS.put_key('people/%s' % name, 'text/plain', 'ref', '', 'text/plain', token, replace=True)
        # Create password
        COMMANDS.accessControl('setUserPassword', token=token, username=name, password=password)
        # Create capabilities
        # xxxjack
        COMMANDS.save(token)
        if returnTo:
            raise web.seeother(returnTo)
        return ''
        
    def delete(self, token=None, name=None, returnTo=None):
        if not NAME_RE.match(name):
            raise myWebError('400 Illegal name for user')
        if not DATABASE_ACCESS.get_key('identities/%s' % name, 'application/x-python-object', 'multi', token):
            raise myWebError('404 user %s does not exist' % name)
        DATABASE_ACCESS.delete_key('people/%s' % name, token)
        # xxxjack delete or save all capabilities
        DATABASE_ACCESS.delete_key('identities/%s' % name, token)
        COMMANDS.save(token)
        if returnTo:
            raise web.seeother(returnTo)
        return ''
        
    def password(self, token=None, name=None, password=None, returnTo=None):
        if not NAME_RE.match(name):
            raise myWebError('400 Illegal name for user')
        if not DATABASE_ACCESS.get_key('identities/%s' % name, 'application/x-python-object', 'multi', token):
            raise myWebError('404 user %s does not exist' % name)
        COMMANDS.accessControl('setUserPassword', token=token, username=name, password=password)
        COMMANDS.save(token)
        if returnTo:
            raise web.seeother(returnTo)
        return ''
    

def igorPlugin(pluginName, pluginData):
    return UserPlugin()
    

@@ -397,32 +397,36 @@
 lf, token=None, 
+user
 name=None, passw
@@ -472,32 +472,36 @@
 t NAME_RE.match(
+user
 name):%0A         
@@ -594,32 +594,36 @@
 dentities/%25s' %25 
+user
 name, 'applicati
@@ -795,24 +795,28 @@
 ities/%25s' %25 
+user
 name, 'text/
@@ -940,24 +940,28 @@
 eople/%25s' %25 
+user
 name, 'text/
@@ -1104,32 +1104,36 @@
 token, username=
+user
 name, password=p
@@ -1179,25 +1179,651 @@
         
-# xxxjack
+COMMANDS.accessControl('newToken', %0A            token=token, %0A            tokenId='admin-data',%0A            newOwner='identities/%25s' %25 username, %0A            newPath='/data/identities/%25s' %25 username,%0A            get='descendant-or-self', %0A            put='descendant', %0A            post='descendant', %0A            delete='descendant')%0A        COMMANDS.accessControl('newToken', %0A            token=token, %0A            tokenId='admin-data',%0A            newOwner='identities/%25s' %25 username, %0A            newPath='/data/people/%25s' %25 username, %0A            put='descendant', %0A            post='descendant', %0A            delete='descendant')
 %0A       
@@ -1958,32 +1958,36 @@
 lf, token=None, 
+user
 name=None, retur
@@ -2018,32 +2018,36 @@
 t NAME_RE.match(
+user
 name):%0A         
@@ -2144,32 +2144,36 @@
 dentities/%25s' %25 
+user
 name, 'applicati
@@ -2262,32 +2262,36 @@
 es not exist' %25 
+user
 name)%0A        DA
@@ -2325,24 +2325,28 @@
 eople/%25s' %25 
+user
 name, token)
@@ -2359,16 +2359,8 @@
    #
- xxxjack
  del
@@ -2380,32 +2380,71 @@
 ll capabilities%0A
+        # xxxjack to be implemented...%0A
         DATABASE
@@ -2476,24 +2476,28 @@
 ities/%25s' %25 
+user
 name, token)
@@ -2650,16 +2650,20 @@
 n=None, 
+user
 name=Non
@@ -2725,16 +2725,20 @@
 E.match(
+user
 name):%0A 
@@ -2847,24 +2847,28 @@
 ities/%25s' %25 
+user
 name, 'appli
@@ -2969,16 +2969,20 @@
 xist' %25 
+user
 name)%0A  
@@ -3051,16 +3051,20 @@
 sername=
+user
 name, pa

Add tokens for new users