| 565 | | class UserProfileManager(UpdateInfoBaseModelManager): |
| 566 | | def create_user_profile(self, request, user): |
| 567 | | userprofile, created = self.get_or_create(request, user=user) |
| 568 | | if created: |
| 569 | | request.user.message_set.create( |
| 570 | | message="UserProfile entry for user '%s' created." % user |
| 571 | | ) |
| 572 | | |
| 573 | | if not user.is_superuser: |
| 574 | | # Info: superuser can automaticly access all sites |
| | 570 | |
| | 571 | |
| | 572 | |
| | 573 | class UserProfile(UpdateInfoBaseModel): |
| | 574 | """ |
| | 575 | Stores additional information about PyLucid users |
| | 576 | http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users |
| | 577 | |
| | 578 | Created via post_save signal, if a new user created. |
| | 579 | """ |
| | 580 | user = models.ForeignKey(User, unique=True, related_name="%(class)s_user") |
| | 581 | |
| | 582 | sha_login_checksum = models.CharField(max_length=192, |
| | 583 | help_text="Checksum for PyLucid JS-SHA-Login" |
| | 584 | ) |
| | 585 | sha_login_salt = models.CharField(max_length=5, |
| | 586 | help_text="Salt value for PyLucid JS-SHA-Login" |
| | 587 | ) |
| | 588 | |
| | 589 | site = models.ManyToManyField(Site, |
| | 590 | help_text="User can access only these sites." |
| | 591 | ) |
| | 592 | |
| | 593 | def set_sha_login_password(self, request, raw_password): |
| | 594 | """ |
| | 595 | create salt+checksum for JS-SHA-Login. |
| | 596 | see also: http://www.pylucid.org/_goto/8/JS-SHA-Login/ |
| | 597 | """ |
| | 598 | raw_password = str(raw_password) |
| | 599 | salt, sha_checksum = crypt.make_sha_checksum2(raw_password) |
| | 600 | self.sha_login_salt = salt |
| | 601 | self.sha_login_checksum = sha_checksum |
| | 602 | request.user.message_set.create( |
| | 603 | message="SHA Login salt+checksum created for user '%s'." % self.user |
| | 604 | ) |
| | 605 | |
| | 606 | def __unicode__(self): |
| | 607 | return u"UserProfile for user '%s'" % self.user.username |
| | 608 | |
| | 609 | def site_info(self): |
| | 610 | """ for pylucid.admin.UserProfileAdmin.list_display """ |
| | 611 | sites = self.site.all() |
| | 612 | return ", ".join([site.name for site in sites]) |
| | 613 | |
| | 614 | class Meta: |
| | 615 | ordering = ("user",) |
| | 616 | |
| | 617 | |
| | 618 | #______________________________________________________________________________ |
| | 619 | # Create user profile via signals |
| | 620 | |
| | 621 | def create_user_profile(sender, **kwargs): |
| | 622 | """ signal handler: creating user profile, after a new user created. """ |
| | 623 | user = kwargs["instance"] |
| | 624 | request = ThreadLocal.get_current_request() |
| | 625 | |
| | 626 | userprofile, created = UserProfile.objects.get_or_create(request, user=user) |
| | 627 | if created: |
| | 628 | request.user.message_set.create(message="UserProfile entry for user '%s' created." % user) |
| | 629 | |
| | 630 | if not user.is_superuser: # Info: superuser can automaticly access all sites |
| 579 | | ) |
| 580 | | |
| 581 | | def delete(self, request, user): |
| 582 | | userprofile = self.get(user=user) |
| 583 | | userprofile.delete() |
| 584 | | request.user.message_set.create( |
| 585 | | message="UserProfile entry from user '%s' deleted." % user |
| 586 | | ) |
| 587 | | |
| 588 | | |
| 589 | | class UserProfile(UpdateInfoBaseModel): |
| 590 | | """ |
| 591 | | Stores additional information about PyLucid users |
| 592 | | http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users |
| 593 | | |
| 594 | | We hacked into django.contrib.auth.admin.UserAdmin in pylucid/admin.py for |
| 595 | | create/delete UserProfile entries. |
| 596 | | """ |
| 597 | | objects = UserProfileManager() |
| 598 | | |
| 599 | | user = models.ForeignKey(User, unique=True, related_name="%(class)s_user") |
| 600 | | |
| 601 | | sha_login_checksum = models.CharField(max_length=192, |
| 602 | | help_text="Checksum for PyLucid JS-SHA-Login" |
| 603 | | ) |
| 604 | | sha_login_salt = models.CharField(max_length=5, |
| 605 | | help_text="Salt value for PyLucid JS-SHA-Login" |
| 606 | | ) |
| 607 | | |
| 608 | | site = models.ManyToManyField(Site, |
| 609 | | help_text="User can access only these sites." |
| 610 | | ) |
| 611 | | |
| 612 | | def __unicode__(self): |
| 613 | | return u"UserProfile for user '%s'" % self.user.username |
| 614 | | |
| 615 | | def site_info(self): |
| 616 | | """ for pylucid.admin.UserProfileAdmin.list_display """ |
| 617 | | sites = self.site.all() |
| 618 | | return ", ".join([site.name for site in sites]) |
| 619 | | |
| 620 | | class Meta: |
| 621 | | ordering = ("user",) |
| | 635 | ) |
| | 636 | |
| | 637 | signals.post_save.connect(create_user_profile, sender=User) |
| | 638 | |
| | 639 | |
| | 640 | #______________________________________________________________________________ |
| | 641 | """ |
| | 642 | We make a Monkey-Patch and change the method set_password() from |
| | 643 | the model class django.contrib.auth.models.User. |
| | 644 | We need the raw plaintext password, this is IMHO not available via signals. |
| | 645 | """ |
| | 646 | |
| | 647 | # Save the original method |
| | 648 | orig_set_password = User.set_password |
| | 649 | |
| | 650 | |
| | 651 | def set_password(user, raw_password): |
| | 652 | #print "set_password() debug:", user, raw_password |
| | 653 | if user.id == None: |
| | 654 | # It is a new user. We must save the django user accound first to get a |
| | 655 | # existing user object with a ID and then the JS-SHA-Login Data can assign to it. |
| | 656 | user.save() |
| | 657 | |
| | 658 | # Use the original method to set the django User password: |
| | 659 | orig_set_password(user, raw_password) |
| | 660 | |
| | 661 | user_profile = user.get_profile() |
| | 662 | request = ThreadLocal.get_current_request() |
| | 663 | |
| | 664 | # Save the password for the JS-SHA-Login: |
| | 665 | user_profile.set_sha_login_password(request, raw_password) |
| | 666 | user_profile.save(request) |
| | 667 | |
| | 668 | |
| | 669 | # replace the method |
| | 670 | User.set_password = set_password |