|
1 | | -import csv |
2 | | -import re |
3 | | - |
4 | | -from .models import APIUser |
5 | | -from django.contrib.auth.models import User |
6 | | - |
7 | | - |
8 | | -class APIUserManagerError(Exception): |
9 | | - pass |
10 | | - |
11 | | - |
12 | | -class UserExists(APIUserManagerError): |
13 | | - pass |
14 | | - |
15 | | - |
16 | | -class UserDoesNotExist(APIUserManagerError): |
17 | | - pass |
18 | | - |
19 | | - |
20 | | -def get_api_user(username): |
21 | | - api_user = None |
22 | | - try: |
23 | | - api_user = APIUser.objects.get(user__username=username) |
24 | | - except APIUser.DoesNotExist: |
25 | | - raise APIUserManagerError('APIUser {} does not exist.' |
26 | | - ''.format(username)) |
27 | | - |
28 | | - return api_user |
29 | | - |
30 | | - |
31 | | -def create_api_user(username, secret, permissions=None, default=False, |
32 | | - email='', django_password=None, first_name='', |
33 | | - last_name=''): |
34 | | - user = None |
35 | | - api_user = None |
36 | | - |
37 | | - try: |
38 | | - api_user = get_api_user(username) |
39 | | - except APIUserManagerError: |
40 | | - pass |
41 | | - else: |
42 | | - raise UserExists('APIUser {} already exists.'.format(username)) |
43 | | - |
44 | | - try: |
45 | | - user = User.objects.get(username=username) |
46 | | - except User.DoesNotExist: |
47 | | - if not django_password: |
48 | | - raise UserDoesNotExist('Django User {} does not exist and ' |
49 | | - 'must be created. You must supply a ' |
50 | | - 'django_password argument.' |
51 | | - ''.format(username)) |
52 | | - user = User.objects.create_user(username, email, django_password) |
53 | | - user.first_name = first_name |
54 | | - user.last_name = last_name |
55 | | - user.save() |
56 | | - |
57 | | - api_user = APIUser(user=user) |
58 | | - api_user.set_secret(secret) |
59 | | - api_user.set_permissions(permissions=permissions, default=default) |
60 | | - return api_user |
61 | | - |
62 | | - |
63 | | -def update_api_user(username, secret=None, permissions=None, default=False, |
64 | | - email='', django_password=None, first_name='', |
65 | | - last_name=''): |
66 | | - try: |
67 | | - api_user = get_api_user(username) |
68 | | - except APIUserManagerError: |
69 | | - raise UserDoesNotExist('API User {} does not exist.'.format(username)) |
70 | | - |
71 | | - if (secret): |
72 | | - api_user.set_secret(secret) |
73 | | - if (permissions): |
74 | | - api_user.set_permissions(permissions=permissions, default=default) |
75 | | - |
76 | | - django_user_changed = False |
77 | | - if (email and email != api_user.user.email): |
78 | | - api_user.user.email = email |
79 | | - django_user_changed = True |
80 | | - if (first_name and first_name != api_user.user.first_name): |
81 | | - api_user.user.first_name = first_name |
82 | | - django_user_changed = True |
83 | | - if (last_name and last_name != api_user.user.last_name): |
84 | | - api_user.user.last_name = last_name |
85 | | - django_user_changed = True |
86 | | - if (django_password and not api_user.user.check_password(django_password)): |
87 | | - api_user.user.set_password(django_password) |
88 | | - django_user_changed = True |
89 | | - |
90 | | - if django_user_changed: |
91 | | - api_user.user.save() |
92 | | - return api_user |
93 | | - |
94 | | - |
95 | | -def set_api_user_secret(username, secret): |
96 | | - api_user = get_api_user(username) |
97 | | - api_user.set_secret(secret) |
98 | | - return api_user |
99 | | - |
100 | | - |
101 | | -def set_api_user_permissions(username, permissions, default=False): |
102 | | - api_user = get_api_user(username) |
103 | | - api_user.set_permissions(permissions=permissions, default=default) |
104 | | - return api_user |
105 | | - |
106 | | - |
107 | | -def batch_create_update_api_users(filepath, default=None): |
108 | | - """ |
109 | | - This will open a csv file provided by filepath and batch create API |
110 | | - users based on its contents. Column names should be provided that |
111 | | - match the args to create_api_user (except default). If the API user |
112 | | - already exists, then this will attempt to update the secret and the |
113 | | - permissions for that user. |
114 | | - """ |
115 | | - data = [] |
116 | | - with open(filepath, 'r') as csvfile: |
117 | | - permreader = csv.DictReader(csvfile) |
118 | | - for row in permreader: |
119 | | - row_data = {} |
120 | | - try: |
121 | | - row_data['username'] = row['username'] |
122 | | - except KeyError: |
123 | | - raise APIUserManagerError('Your csv file must include a ' |
124 | | - '"username" column.') |
125 | | - row_data['secret'] = row.get('secret', None) |
126 | | - row_data['django_password'] = row.get('django_password', None) |
127 | | - row_data['first_name'] = row.get('first_name', None) |
128 | | - row_data['last_name'] = row.get('last_name', None) |
129 | | - row_data['email'] = row.get('email', None) |
130 | | - |
131 | | - permissions = {} |
132 | | - |
133 | | - for key, val in row.iteritems(): |
134 | | - if re.match(r'^permission_', key): |
135 | | - if val in ('True', 'true', 'TRUE', 'T', 't', '1'): |
136 | | - val = True |
137 | | - else: |
138 | | - val = False |
139 | | - permissions[re.sub(r'^permission_', '', key)] = val |
140 | | - |
141 | | - if not permissions and default is not None: |
142 | | - permissions = default |
143 | | - |
144 | | - row_data['permissions'] = permissions |
145 | | - data.append(row_data) |
146 | | - |
147 | | - for row in data: |
148 | | - try: |
149 | | - create_api_user(row['username'], row['secret'], |
150 | | - permissions=row['permissions'], |
151 | | - email=row['email'], |
152 | | - django_password=row['django_password'], |
153 | | - first_name=row['first_name'], |
154 | | - last_name=row['last_name'], default=default) |
155 | | - except UserExists: |
156 | | - update_api_user(row['username'], row['secret'], |
157 | | - permissions=row['permissions'], |
158 | | - email=row['email'], |
159 | | - django_password=row['django_password'], |
160 | | - first_name=row['first_name'], |
161 | | - last_name=row['last_name'], default=default) |
162 | | - except UserDoesNotExist: |
163 | | - print ('User {} does not exist and no django_password was ' |
164 | | - 'provided. Skipping.'.format(row['username'])) |
0 commit comments