Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / auth.py @ f6ff3033

History | View | Annotate | Download (3.2 kB)

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