Commit 367a6f06 authored by Michal Čihař's avatar Michal Čihař

Create anonymous user for anonymous user privileges

parent 2206df51
...@@ -18,14 +18,16 @@ ...@@ -18,14 +18,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from django.db import models from django.db import models, IntegrityError
from django.dispatch import receiver from django.dispatch import receiver
from django.conf import settings from django.conf import settings
from django.contrib.auth.signals import user_logged_in from django.contrib.auth.signals import user_logged_in
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.utils.translation import ugettext_lazy as _, gettext from django.utils.translation import ugettext_lazy as _, gettext
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.models import Group, Permission, User from django.contrib.auth.models import (
Group, User, UNUSABLE_PASSWORD, Permission
)
from django.db.models.signals import post_syncdb from django.db.models.signals import post_syncdb
from registration.signals import user_registered from registration.signals import user_registered
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
...@@ -41,6 +43,7 @@ from trans.util import ( ...@@ -41,6 +43,7 @@ from trans.util import (
get_user_display, get_site_url, get_distinct_translations get_user_display, get_site_url, get_distinct_translations
) )
import weblate import weblate
from weblate.appsettings import ANONYMOUS_USER_NAME
def notify_merge_failure(subproject, error, status): def notify_merge_failure(subproject, error, status):
...@@ -564,9 +567,9 @@ def create_groups(update): ...@@ -564,9 +567,9 @@ def create_groups(update):
''' '''
Creates standard groups and gives them permissions. Creates standard groups and gives them permissions.
''' '''
group, created = Group.objects.get_or_create(name='Guests') guest_group, created = Group.objects.get_or_create(name='Guests')
if created or update: if created or update:
group.permissions.add( guest_group.permissions.add(
Permission.objects.get(codename='can_see_git_repository'), Permission.objects.get(codename='can_see_git_repository'),
) )
...@@ -617,6 +620,25 @@ def create_groups(update): ...@@ -617,6 +620,25 @@ def create_groups(update):
Permission.objects.get(codename='delete_comment'), Permission.objects.get(codename='delete_comment'),
) )
try:
anon_user, created = User.objects.get_or_create(
username=ANONYMOUS_USER_NAME,
password=UNUSABLE_PASSWORD,
is_active=False,
)
if created or update:
anon_user.groups.clear()
anon_user.groups.add(guest_group)
except IntegrityError as error:
raise ValueError(
'Anonymous user (%s) already exists and enabled, '
'please change ANONYMOUS_USER_NAME setting.\n'
'Raised error was: %s' % (
ANONYMOUS_USER_NAME,
repr(error)
)
)
def move_users(): def move_users():
''' '''
......
...@@ -32,6 +32,16 @@ be an integer. ...@@ -32,6 +32,16 @@ be an integer.
This is actually django-registration settings. This is actually django-registration settings.
.. setting:: ANONYMOUS_USER_NAME
ANONYMOUS_USER_NAME
-------------------
User name of user for definining privileges of not logged in user.
.. seealso:: :ref:`privileges`
.. setting:: AUTO_LOCK .. setting:: AUTO_LOCK
AUTO_LOCK AUTO_LOCK
......
...@@ -142,3 +142,6 @@ SCRIPT_CHOICES = [ ...@@ -142,3 +142,6 @@ SCRIPT_CHOICES = [
# Font for charts and widgets # Font for charts and widgets
TTF_PATH = get('TTF_PATH', os.path.join(WEB_ROOT, 'ttf')) TTF_PATH = get('TTF_PATH', os.path.join(WEB_ROOT, 'ttf'))
# Anonymous user name
ANONYMOUS_USER_NAME = get('ANONYMOUS_USER_NAME', 'anonymous')
...@@ -349,6 +349,9 @@ ACCOUNT_ACTIVATION_DAYS = 7 ...@@ -349,6 +349,9 @@ ACCOUNT_ACTIVATION_DAYS = 7
# Profile module # Profile module
AUTH_PROFILE_MODULE = 'accounts.Profile' AUTH_PROFILE_MODULE = 'accounts.Profile'
# Anonymous user name
ANONYMOUS_USER_NAME = 'anonymous'
# Sending HTML in mails # Sending HTML in mails
EMAIL_SEND_HTML = False EMAIL_SEND_HTML = False
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment