Index: /pykota/trunk/pykota/storages/ldapstorage.py
===================================================================
--- /pykota/trunk/pykota/storages/ldapstorage.py (revision 1030)
+++ /pykota/trunk/pykota/storages/ldapstorage.py (revision 1031)
@@ -21,4 +21,7 @@
 #
 # $Log$
+# Revision 1.9  2003/06/16 11:59:09  jalet
+# More work on LDAP
+#
 # Revision 1.8  2003/06/15 22:26:52  jalet
 # More work on LDAP
@@ -86,4 +89,6 @@
         except ldap.SERVER_DOWN :    
             raise PyKotaStorageError, "LDAP backend for PyKota seems to be down !" # TODO : translate
+        except ldap.LDAPError :    
+            raise PyKotaStorageError, "Unable to connect to LDAP server %s as %s." % (host, user) # TODO : translate
         else :    
             self.closed = 0
@@ -116,4 +121,6 @@
             raise PyKotaStorageError, _("Search for %s(%s) from %s(scope=%s) returned no answer.") % (key, fields, base, scope)
         else :     
+            if self.debug :
+                self.tool.logger.log_message("QUERY : Result : %s" % result, "debug")
             return result
             
@@ -155,5 +162,5 @@
     def getPrinterName(self, printerid) :        
         """Returns a printerid given a printer id."""
-        result = self.doSearch("objectClass=*", ["pykotaPrinterName"], base=printerid, scope=ldap.SCOPE_BASE)
+        result = self.doSearch("objectClass=pykotaPrinter", ["pykotaPrinterName"], base=printerid, scope=ldap.SCOPE_BASE)
         if result :
             return result[0][1]["pykotaPrinterName"][0]
@@ -167,5 +174,9 @@
     def setPrinterPrices(self, printerid, perpage, perjob) :
         """Sets prices per job and per page for a given printer."""
-        raise PyKotaStorageError, "Not implemented !"
+        fields = { 
+                   "pykotaPricePerPage" : str(perpage),
+                   "pykotaPricePerJob" : str(perjob),
+                 } 
+        return self.doModify(printerid, fields)
     
     def getUserId(self, username) :
@@ -174,4 +185,10 @@
         if result :
             return result[0][0]
+            
+    def getUserName(self, userid) :
+        """Returns a username given a userid."""
+        result = self.doSearch("objectClass=pykotaAccount", ["pykotaUserName"], base=userid, scope=ldap.SCOPE_BASE)
+        if result :
+            return result[0][1]["pykotaUserName"][0]
             
     def getGroupId(self, groupname) :
@@ -220,5 +237,9 @@
     def getUserGroupsNames(self, userid) :        
         """Returns the list of groups' names the user is a member of."""
-        raise PyKotaStorageError, "Not implemented !"
+        username = self.getUserName(userid)
+        if username :
+            result = self.doSearch("(&(objectClass=pykotaGroup)(%s=%s))" % (self.info["groupmembers"], username), [self.info["grouprdn"]], base=self.info["groupbase"])
+            if result :
+                return [v[self.info["grouprdn"]][0] for (k, v) in result]
         
     def addPrinter(self, printername) :        
@@ -269,5 +290,6 @@
                  } 
         dn = "cn=%s,%s" % (uuid, self.info["userquotabase"])
-        return self.doAdd(dn, fields)
+        self.doAdd(dn, fields)
+        return (dn, printerid)
         
     def addGroupPQuota(self, groupname, printerid) :
@@ -283,9 +305,22 @@
                  } 
         dn = "cn=%s,%s" % (uuid, self.info["groupquotabase"])
-        return self.doAdd(dn, fields)
-        
-    def increaseUserBalance(self, userid, amount) :    
+        self.doAdd(dn, fields)
+        return (dn, printerid)
+        
+    def increaseUserBalance(self, userquotaid, amount) :    
         """Increases (or decreases) an user's account balance by a given amount."""
-        raise PyKotaStorageError, "Not implemented !"
+        balance = self.getUserBalance(userquotaid)
+        if balance :
+            (newbal, newpaid) = [(float(v) + float(amount)) for v in balance]
+            result = self.doSearch("objectClass=pykotaUserPQuota", ["pykotaUserName"], base=userquotaid, scope=ldap.SCOPE_BASE)
+            if result :
+                username = result[0][1]["pykotaUserName"][0]
+                if username :
+                    result = self.doSearch("(&(objectClass=pykotaAccountBalance)(pykotaUserName=%s))" % username , [ "pykotaBalance", "pykotaLifeTimePaid"])
+                    fields = {
+                               "pykotaBalance" : str(newbal),
+                               "pykotaLifeTimePaid" : str(newpaid),
+                             }
+                    return self.doModify(result[0][0], fields)         
         
     def getUserBalance(self, userquotaid) :    
@@ -349,15 +384,31 @@
                 return result[0][1]["pykotaLimitBy"][0]
         
-    def setUserBalance(self, userid, balance) :    
+    def setUserBalance(self, userquotaid, balance) :    
         """Sets the account balance for a given user to a fixed value."""
-        raise PyKotaStorageError, "Not implemented !"
-        
-    def limitUserBy(self, userid, limitby) :    
+        oldbalance = self.getUserBalance(userquotaid)
+        if oldbalance :
+            (oldbal, oldpaid) = oldbalance
+            difference = balance - oldbal
+            return self.increaseUserBalance(userquotaid, difference)
+        
+    def limitUserBy(self, userquotaid, limitby) :    
         """Limits a given user based either on print quota or on account balance."""
-        raise PyKotaStorageError, "Not implemented !"
-        
-    def limitGroupBy(self, groupid, limitby) :    
+        result = self.doSearch("objectClass=pykotaUserPQuota", ["pykotaUserName"], base=userquotaid, scope=ldap.SCOPE_BASE)
+        if result :
+            username = result[0][1]["pykotaUserName"][0]
+            fields = {
+                       "pykotaLimitBy" : limitby,
+                     }
+            self.doModify(self.getUserId(username), fields)
+        
+    def limitGroupBy(self, groupquotaid, limitby) :    
         """Limits a given group based either on print quota or on sum of its users' account balances."""
-        raise PyKotaStorageError, "Not implemented !"
+        result = self.doSearch("objectClass=pykotaGroupPQuota", ["pykotaGroupName"], base=groupquotaid, scope=ldap.SCOPE_BASE)
+        if result :
+            groupname = result[0][1]["pykotaGroupName"][0]
+            fields = {
+                       "pykotaLimitBy" : limitby,
+                     }
+            self.doModify(self.getGroupId(groupname), fields)
         
     def setUserPQuota(self, userquotaid, printerid, softlimit, hardlimit) :
@@ -377,15 +428,31 @@
         return self.doModify(groupquotaid, fields)
         
-    def resetUserPQuota(self, userid, printerid) :    
+    def resetUserPQuota(self, userquotaid, printerid) :    
         """Resets the page counter to zero for a user on a printer. Life time page counter is kept unchanged."""
-        raise PyKotaStorageError, "Not implemented !"
-        
-    def resetGroupPQuota(self, groupid, printerid) :    
+        fields = {
+                   "pykotaPageCounter" : "0",
+                   "pykotaDateLimit" : "None",
+                 }
+        return self.doModify(userquotaid, fields)      
+        
+    def resetGroupPQuota(self, groupquotaid, printerid) :    
         """Resets the page counter to zero for a group on a printer. Life time page counter is kept unchanged."""
-        raise PyKotaStorageError, "Not implemented !"
-        
-    def updateUserPQuota(self, userid, printerid, pagecount) :
+        fields = {
+                   "pykotaPageCounter" : "0",
+                   "pykotaDateLimit" : "None",
+                 }
+        return self.doModify(groupquotaid, fields)      
+        
+    def updateUserPQuota(self, userquotaid, printerid, pagecount) :
         """Updates the used user Quota information given (userid, printerid) and a job size in pages."""
-        raise PyKotaStorageError, "Not implemented !"
+        jobprice = self.computePrinterJobPrice(printerid, pagecount)
+        result = self.doSearch("objectClass=pykotaUserPQuota", ["pykotaPageCounter", "pykotaLifePageCounter"], base=userquotaid, scope=ldap.SCOPE_BASE)
+        if result :
+            oldfields = result[0][1]
+            fields = {
+                       "pykotaPageCounter" : str(pagecount + int(oldfields["pykotaPageCounter"][0])),
+                       "pykotaLifePageCounter" : str(pagecount + int(oldfields["pykotaLifePageCounter"][0])),
+                     }  
+            return self.doModify(userquotaid, fields)         
         
     def getUserPQuota(self, userquotaid, printerid) :
@@ -432,11 +499,17 @@
             return quota
         
-    def setUserDateLimit(self, userid, printerid, datelimit) :
+    def setUserDateLimit(self, userquotaid, printerid, datelimit) :
         """Sets the limit date for a soft limit to become an hard one given (userid, printerid)."""
-        raise PyKotaStorageError, "Not implemented !"
-        
-    def setGroupDateLimit(self, groupid, printerid, datelimit) :
+        fields = {
+                   "pykotaDateLimit" : "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second),
+                 }
+        return self.doModify(userquotaid, fields)
+        
+    def setGroupDateLimit(self, groupquotaid, printerid, datelimit) :
         """Sets the limit date for a soft limit to become an hard one given (groupid, printerid)."""
-        raise PyKotaStorageError, "Not implemented !"
+        fields = {
+                   "pykotaDateLimit" : "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second),
+                 }
+        return self.doModify(groupquotaid, fields)
         
     def addJobToHistory(self, jobid, userid, printerid, pagecounter, action) :
Index: /pykota/trunk/bin/edpykota
===================================================================
--- /pykota/trunk/bin/edpykota (revision 1023)
+++ /pykota/trunk/bin/edpykota (revision 1031)
@@ -23,4 +23,7 @@
 #
 # $Log$
+# Revision 1.46  2003/06/16 11:59:09  jalet
+# More work on LDAP
+#
 # Revision 1.45  2003/06/11 19:32:00  jalet
 # Severe bug wrt account balance setting should be corrected.
@@ -428,15 +431,15 @@
             else :   
                 if options["groups"] :    
-                    allidnames = self.storage.getPrinterGroups(printerid)
+                    allidnames = self.storage.getPrinterGroups(printerid) or []
                 else :    
-                    allidnames = self.storage.getPrinterUsers(printerid)
+                    allidnames = self.storage.getPrinterUsers(printerid) or []
             for (ident, name) in [(i, n) for (i, n) in allidnames if self.matchString(n, names)]:
                 if options["groups"] :
-                    if not gwaschanged.has_key(ident) :
-                        gwaschanged[ident] = {}
+                    if not gwaschanged.has_key(name) :
+                        gwaschanged[name] = {}
                     quota = self.storage.getGroupPQuota(ident, printerid)
                 else :
-                    if not uwaschanged.has_key(ident) :
-                        uwaschanged[ident] = {"ingroups": []}
+                    if not uwaschanged.has_key(name) :
+                        uwaschanged[name] = {"ingroups": []}
                     quota = self.storage.getUserPQuota(ident, printerid)
                 if quota is None :
@@ -474,7 +477,7 @@
                                 self.storage.resetGroupPQuota(ident, printerid)
                             if limitby :
-                                if gwaschanged[ident].get("limitby") is None :
+                                if gwaschanged[name].get("limitby") is None :
                                     self.storage.limitGroupBy(ident, limitby)
-                                    gwaschanged[ident]["limitby"] = limitby
+                                    gwaschanged[name]["limitby"] = limitby
                             self.warnGroupPQuota(name, printer)    
                         else :
@@ -484,20 +487,20 @@
                                 self.storage.resetUserPQuota(ident, printerid)
                             if limitby :
-                                if uwaschanged[ident].get("limitby") is None :
+                                if uwaschanged[name].get("limitby") is None :
                                     self.storage.limitUserBy(ident, limitby)
-                                    uwaschanged[ident]["limitby"] = limitby
+                                    uwaschanged[name]["limitby"] = limitby
                             if balance :
-                                if uwaschanged[ident].get("balance") is None :
+                                if uwaschanged[name].get("balance") is None :
                                     if balance.startswith("+") or balance.startswith("-") :
                                         self.storage.increaseUserBalance(ident, balancevalue)
                                     else :
                                         self.storage.setUserBalance(ident, balancevalue)
-                                    uwaschanged[ident]["balance"] = balance
+                                    uwaschanged[name]["balance"] = balance
                             for groupname in groupnames :        
-                                if groupname not in uwaschanged[ident]["ingroups"] :
+                                if groupname not in uwaschanged[name]["ingroups"] :
                                     groupid = self.storage.getGroupId(groupname)
                                     if groupid is not None :
                                         self.storage.addUserToGroup(ident, groupid)
-                                        uwaschanged[ident]["ingroups"].append(groupname)
+                                        uwaschanged[name]["ingroups"].append(groupname)
                                     else :
                                         self.logger.log_message(_("Group %s not found in the PyKota Storage.") % groupname)
