Ticket #9444: anonuser.models.patch
File anonuser.models.patch, 5.6 KB (added by , 16 years ago) |
---|
-
\django\contrib\auth\models.py
old new 147 147 return self.username 148 148 149 149 def get_absolute_url(self): 150 if self.username == 'BuiltinDjangoAnonymousUser': 151 return '/' 152 150 153 return "/users/%s/" % urllib.quote(smart_str(self.username)) 151 154 152 155 def is_anonymous(self): 153 "Always returns False. This is a way of comparing User objects to anonymous users." 156 "Return True if it is the Anonymous users, False if not" 157 if self.username == 'BuiltinDjangoAnonymousUser': 158 return True 154 159 return False 155 160 156 161 def is_authenticated(self): 157 """ Always return True. This is a way to tell if the user has been authenticated in templates.162 """Return True for loged in users and False of the Anon Users. This is a way to tell if the user has been authenticated in templates. 158 163 """ 164 if self.username == 'BuiltinDjangoAnonymousUser': 165 return False 159 166 return True 160 167 161 168 def get_full_name(self): 162 "Returns the first_name plus the last_name, with a space in between." 169 "Returns the first_name plus the last_name, with a space in between." 163 170 full_name = u'%s %s' % (self.first_name, self.last_name) 164 171 return full_name.strip() 165 172 166 173 def set_password(self, raw_password): 174 """ 175 Set the password for the user, just return if it is the Anon User 176 """ 177 if self.username == 'BuiltinDjangoAnonymousUser': 178 return 167 179 import random 168 180 algo = 'sha1' 169 181 salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5] … … 174 186 """ 175 187 Returns a boolean of whether the raw_password was correct. Handles 176 188 encryption formats behind the scenes. 189 Always return False for the Anon user since people cannot login as that user 177 190 """ 191 if self.username == 'BuiltinDjangoAnonymousUser': 192 return False 178 193 # Backwards-compatibility check. Older passwords won't include the 179 194 # algorithm or salt. 180 195 if '$' not in self.password: … … 197 212 """ 198 213 Returns a list of permission strings that this user has through 199 214 his/her groups. This method queries all available auth backends. 215 Not special casing the Anon user for groups or permissions because 216 the whole point of actualy having the Anon user is so you can setup 217 permissions for it 200 218 """ 201 219 permissions = set() 202 220 for backend in auth.get_backends(): … … 217 235 queries all available auth backends, but returns immediately if any 218 236 backend returns True. Thus, a user who has permission from a single 219 237 auth backend is assumed to have permission in general. 238 Not special casing the Anon user for groups or permissions because 239 the whole point of actualy having the Anon user is so you can setup 240 permissions for it 220 241 """ 221 242 # Inactive users have no permissions. 222 243 if not self.is_active: … … 234 255 return False 235 256 236 257 def has_perms(self, perm_list): 237 """Returns True if the user has each of the specified permissions.""" 258 """ 259 Returns True if the user has each of the specified permissions. 260 Not special casing the Anon user for groups or permissions because 261 the whole point of actualy having the Anon user is so you can setup 262 permissions for it 263 """ 238 264 for perm in perm_list: 239 265 if not self.has_perm(perm): 240 266 return False … … 244 270 """ 245 271 Returns True if the user has any permissions in the given app 246 272 label. Uses pretty much the same logic as has_perm, above. 273 Not special casing the Anon user for groups or permissions because 274 the whole point of actualy having the Anon user is so you can setup 275 permissions for it 247 276 """ 248 277 if not self.is_active: 249 278 return False … … 265 294 return messages 266 295 267 296 def email_user(self, subject, message, from_email=None): 268 "Sends an e-mail to this User." 297 """ 298 Sends an e-mail to this User. 299 Dont try to sent to the Anon User 300 """ 301 if self.username == 'BuiltinDjangoAnonymousUser': 302 return 269 303 from django.core.mail import send_mail 270 304 send_mail(subject, message, from_email, [self.email]) 271 305 … … 273 307 """ 274 308 Returns site-specific profile for this user. Raises 275 309 SiteProfileNotAvailable if this site does not allow profiles. 310 Anon cannot have a profile either 276 311 """ 312 if self.username == 'BuiltinDjangoAnonymousUser': 313 raise SiteProfileNotAvailable 314 277 315 if not hasattr(self, '_profile_cache'): 278 316 from django.conf import settings 279 317 if not getattr(settings, 'AUTH_PROFILE_MODULE', False): … … 302 340 def __unicode__(self): 303 341 return self.message 304 342 305 class AnonymousUser (object):343 class AnonymousUserClass(object): 306 344 id = None 307 345 username = '' 308 346 is_staff = False … … 366 404 367 405 def is_authenticated(self): 368 406 return False 407 408 def AnonymousUser(): 409 try: 410 return User.objects.get(username__exact='BuiltinDjangoAnonymousUser') 411 except: 412 return AnonymousUserClass()