changeset 1695:5a93f13c1e76

core (memory): asyncCreateProfile fix: a fake session is created to set general password param
author Goffi <goffi@goffi.org>
date Fri, 27 Nov 2015 16:52:14 +0100
parents 3c608d660f0b
children 9a7a27c44611
files src/memory/memory.py
diffstat 1 files changed, 19 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/memory/memory.py	Fri Nov 27 16:50:29 2015 +0100
+++ b/src/memory/memory.py	Fri Nov 27 16:52:14 2015 +0100
@@ -459,10 +459,11 @@
 
         self.memory_data['Profile_default'] = profile
 
-    def asyncCreateProfile(self, name, password=''):
+    def asyncCreateProfile(self, name, password):
         """Create a new profile
-        @param name: profile name
-        @param password: profile password
+        @param name (unicode): profile name
+        @param password (unicode): profile password
+            Can be empty to disable password
         @return: Deferred
         """
         if not name:
@@ -470,6 +471,9 @@
         if name[0] == '@':
             raise ValueError("A profile name can't start with a '@'")
 
+        if name in self._entities_cache:
+            raise exceptions.ConflictError(u"A session for this profile exists")
+
         d = self.params.asyncCreateProfile(name)
 
         def initPersonalKey(dummy):
@@ -477,8 +481,20 @@
             personal_key = BlockCipher.getRandomKey(base64=True)  # generated once for all and saved in a PersistentDict
             self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, profile=name)  # will be encrypted by setParam
 
+        def startFakeSession(dummy):
+            # avoid ProfileNotConnected exception in setParam
+            self._entities_cache[name] = None
+            self.params.loadIndParams(name)
+
+        def stopFakeSession(dummy):
+            del self._entities_cache[name]
+            self.params.purgeProfile(name)
+
         d.addCallback(initPersonalKey)
+        d.addCallback(startFakeSession)
         d.addCallback(lambda dummy: self.setParam(C.PROFILE_PASS_PATH[1], password, C.PROFILE_PASS_PATH[0], profile_key=name))
+        d.addCallback(stopFakeSession)
+        d.addCallback(lambda dummy: self.auth_sessions.profileDelUnique(name))
         return d
 
     def asyncDeleteProfile(self, name, force=False):