This commit is contained in:
Rob Hudson 2013-10-17 13:19:03 -07:00
Родитель 5e8ec7f93f
Коммит 1edf0751a7
3 изменённых файлов: 24 добавлений и 36 удалений

Просмотреть файл

@ -0,0 +1,2 @@
UPDATE groups SET name='Monolith API', rules='Monolith:API,RevenueStats:View'
WHERE name='Monolith:API' LIMIT 1;

Просмотреть файл

@ -10,14 +10,13 @@ from mkt.site.management.commands.add_test_users import create_user
class Command(BaseCommand):
help = """Create an user with access to the monolith API"""
option_list = BaseCommand.option_list + (
make_option('--overwrite',
action='store_true',
dest='overwrite',
default=False,
help='Overwrite the user if it already exist'),)
make_option(
'--overwrite', action='store_true',
dest='overwrite', default=False,
help='Overwrite the user access token if it already exists'),)
def handle(self, *args, **kw):
create_user('monolith@mozilla.com',
delete_user=kw['overwrite'],
overwrite=kw['overwrite'],
password=settings.MONOLITH_PASSWORD,
permissions=('Monolith:API', ))
group_name='Monolith API')

Просмотреть файл

@ -3,35 +3,28 @@ from datetime import datetime
from optparse import make_option
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
from django.db import transaction
import amo
from access.models import GroupUser, Group
from access.models import Group, GroupUser
from apps.users.models import UserProfile
from mkt.api.models import Access
@transaction.commit_on_success
def create_user(email, password, group_name=None, delete_user=False,
permissions=None):
def create_user(email, password, group_name=None, overwrite=False):
"""Create an user if he doesn't exist already, assign him to a group and
create a token for him.
On token creation, we generate the token key and the token secret. Each of
them are generated in a predictible way: sha512(password + email + 'key') or
sha512(password + email + 'secret').
them are generated in a predictible way: sha512(password + email + 'key')
or sha512(password + email + 'secret').
"""
if delete_user:
users = User.objects.filter(email=email)
if users.exists():
users[0].delete()
# Create the user.
profile, created = UserProfile.objects.get_or_create(
username=email, email=email, source=amo.LOGIN_SOURCE_UNKNOWN,
display_name=email)
username=email, email=email, source=amo.LOGIN_SOURCE_UNKNOWN,
display_name=email)
if created:
profile.create_django_user()
@ -48,35 +41,29 @@ def create_user(email, password, group_name=None, delete_user=False,
# We also want to grant these users access, so let's create tokens for
# them.
if overwrite:
Access.objects.filter(user=profile.user).delete()
if not Access.objects.filter(user=profile.user).exists():
key = hashlib.sha512(password + email + 'key').hexdigest()
secret = hashlib.sha512(password + email + 'secret').hexdigest()
consumer = Access(key=key, secret=secret, user=profile.user)
consumer.save()
if permissions is not None:
for permission in permissions:
group, _ = Group.objects.get_or_create(name=permission,
rules=permission)
GroupUser.objects.get_or_create(group=group, user=profile)
Access.objects.create(key=key, secret=secret, user=profile.user)
class Command(BaseCommand):
help = """Create three users with different profiles (App Review, Admin,
Developer)
help = """Create users with different profiles (App Review, Admin,
Developer, End User)
"""
option_list = BaseCommand.option_list + (
make_option('--clear',
action='store_true',
dest='clear',
default=False,
help='Clear the users before recreating them'),)
make_option(
'--clear', action='store_true', dest='clear', default=False,
help='Clear the user access tokens before recreating them'),)
def handle(self, *args, **kw):
options = {'password': settings.API_PASSWORD}
if kw['clear']:
options['delete_user'] = True
options['overwrite'] = True
create_user('appreviewer@mozilla.com', group_name='App Reviewers',
**options)