root / snf-astakos-app / astakos / im / auth.py @ f6ff3033
History | View | Annotate | Download (3.2 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import uuid |
35 |
import datetime |
36 |
from astakos.im import models |
37 |
|
38 |
|
39 |
def _finalize_astakosuser_object(user, has_signed_terms=False): |
40 |
user.fix_username() |
41 |
if has_signed_terms:
|
42 |
user.has_signed_terms = True
|
43 |
user.date_signed_terms = datetime.datetime.now() |
44 |
|
45 |
user.renew_verification_code() |
46 |
user.uuid = str(uuid.uuid4())
|
47 |
user.renew_token() |
48 |
user.save() |
49 |
|
50 |
|
51 |
def set_local_auth(user): |
52 |
user.add_auth_provider('local', auth_backend='astakos') |
53 |
|
54 |
|
55 |
def make_user(email, first_name="", last_name="", password=None, |
56 |
has_signed_terms=False):
|
57 |
# delete previously unverified accounts
|
58 |
models.AstakosUser.objects.unverified_namesakes(email).delete() |
59 |
|
60 |
user = models.AstakosUser( |
61 |
email=email, first_name=first_name, last_name=last_name, |
62 |
is_active=False)
|
63 |
if password is None: |
64 |
user.set_unusable_password() |
65 |
else:
|
66 |
user.set_password(password) |
67 |
|
68 |
user.date_joined = datetime.datetime.now() |
69 |
_finalize_astakosuser_object(user, has_signed_terms) |
70 |
return user
|
71 |
|
72 |
|
73 |
def make_local_user(email, **kwargs): |
74 |
user = make_user(email, **kwargs) |
75 |
set_local_auth(user) |
76 |
return user
|
77 |
|
78 |
|
79 |
def extend_superuser(user): |
80 |
extended_user = models.AstakosUser(user_ptr_id=user.pk) |
81 |
extended_user.__dict__.update(user.__dict__) |
82 |
_finalize_astakosuser_object(extended_user, has_signed_terms=True)
|
83 |
set_local_auth(extended_user) |
84 |
return extended_user
|
85 |
|
86 |
|
87 |
def fix_superusers(): |
88 |
admins = models.User.objects.filter(is_superuser=True)
|
89 |
fixed = [] |
90 |
for u in admins: |
91 |
try:
|
92 |
models.AstakosUser.objects.get(user_ptr=u.pk) |
93 |
except models.AstakosUser.DoesNotExist:
|
94 |
fixed.append(extend_superuser(u)) |
95 |
return fixed
|