Browse Source

Small updates in Lodel.user module

Yann Weber 9 years ago
parent
commit
cb1da3be7d
1 changed files with 29 additions and 9 deletions
  1. 29
    9
      Lodel/user.py

+ 29
- 9
Lodel/user.py View File

@@ -1,5 +1,10 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
+## @package Lodel.user
4
+# @brief Defines Classes designed to handle users and user's context.
5
+#
6
+# Classes defined in this package are "helpers" for Lodel2 UI
7
+
3 8
 import warnings
4 9
 from Lodel.settings import Settings
5 10
 
@@ -8,6 +13,9 @@ from Lodel.settings import Settings
8 13
 # Class that produce immutable instance representing an identity
9 14
 class UserIdentity(object):
10 15
     
16
+    ## Maintain a reference to a UserIdentity instance that represents an anonymous user
17
+    __anonymous_user = None
18
+
11 19
     ## @brief Constructor
12 20
     # @note produce immutable instance
13 21
     # @param user_id * : user id
@@ -28,7 +36,12 @@ class UserIdentity(object):
28 36
     @property
29 37
     def username(self):
30 38
         return self.__username
31
-
39
+    
40
+    ## @brief getter for fullname
41
+    @property
42
+    def fullname(self):
43
+        return self.__fullname
44
+    
32 45
     @property
33 46
     def is_authenticated(self):
34 47
         return self.__authenticated
@@ -37,18 +50,25 @@ class UserIdentity(object):
37 50
     def is_identified(self):
38 51
         return self.__identified
39 52
     
40
-    ## @brief getter for fullname
41
-    @property
42
-    def fullname(self):
43
-        return self.__fullname
44
-
45 53
     def __repr__(self):
46
-        return "%s(id=%s)" % (self.__username, self.__user_id)
54
+        return "User '{user_id}'( username = '{username}', fullname = '{fullname}', identified : {identified}, authentified : {auth}".format(
55
+                                    user_id = self.__user_id,
56
+                                    username = self.__username,
57
+                                    fullname = self.__fullname,
58
+                                    identified = str(self.__identified),
59
+                                    auth = str(self.__authenticated),
60
+        )
47 61
 
48 62
     def __str__(self):
49 63
         return self.__fullname
50 64
 
51
-anonymous_user = UserIdentity(False, "anonymous", "Anonymous user")
65
+    ## @brief Provide an instance of UserIdentity representing an anonymous user
66
+    @classmethod
67
+    def anonymous(cls):
68
+        if cls.__anonymous_user is None:
69
+            cls.__anonymous_user = UserIdentity(False, "anonymous", "Anonymous user")
70
+        return cls.__anonymous_user
71
+
52 72
 
53 73
 ## @brief Decorator class designed to register user authentication methods
54 74
 #
@@ -183,7 +203,7 @@ class UserContext(object):
183 203
         cls.assert_init()
184 204
         if cls.__identity is False:
185 205
             ret = identification_method.identify(cls.__client_infos)
186
-            cls.__identity = anonymous_user if ret is False else ret
206
+            cls.__identity = UserIdentity.anonymous() if ret is False else ret
187 207
         return cls.__identity
188 208
 
189 209
     ## @brief authenticate a user

Loading…
Cancel
Save