root / logic / users.py @ 583bfaa0
History | View | Annotate | Download (3.6 kB)
1 | 48130e66 | Georgios Gousios | # Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 420f2c20 | Georgios Gousios | #
|
3 | 48130e66 | Georgios Gousios | # Redistribution and use in source and binary forms, with or without
|
4 | 48130e66 | Georgios Gousios | # modification, are permitted provided that the following conditions
|
5 | 48130e66 | Georgios Gousios | # are met:
|
6 | 48130e66 | Georgios Gousios | #
|
7 | 48130e66 | Georgios Gousios | # 1. Redistributions of source code must retain the above copyright
|
8 | 48130e66 | Georgios Gousios | # notice, this list of conditions and the following disclaimer.
|
9 | 48130e66 | Georgios Gousios | #
|
10 | 48130e66 | Georgios Gousios | # 2. Redistributions in binary form must reproduce the above copyright
|
11 | 48130e66 | Georgios Gousios | # notice, this list of conditions and the following disclaimer in the
|
12 | 48130e66 | Georgios Gousios | # documentation and/or other materials provided with the distribution.
|
13 | 420f2c20 | Georgios Gousios | #
|
14 | 48130e66 | Georgios Gousios | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 | 48130e66 | Georgios Gousios | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 | 48130e66 | Georgios Gousios | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 | 48130e66 | Georgios Gousios | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 | 48130e66 | Georgios Gousios | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 | 48130e66 | Georgios Gousios | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 | 48130e66 | Georgios Gousios | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 | 48130e66 | Georgios Gousios | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 | 48130e66 | Georgios Gousios | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 | 48130e66 | Georgios Gousios | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 | 48130e66 | Georgios Gousios | # SUCH DAMAGE.
|
25 | 420f2c20 | Georgios Gousios | #
|
26 | 48130e66 | Georgios Gousios | # The views and conclusions contained in the software and documentation are
|
27 | 48130e66 | Georgios Gousios | # those of the authors and should not be interpreted as representing official
|
28 | 48130e66 | Georgios Gousios | # policies, either expressed or implied, of GRNET S.A.
|
29 | 48130e66 | Georgios Gousios | |
30 | 48130e66 | Georgios Gousios | # Business Logic for working with users
|
31 | 48130e66 | Georgios Gousios | |
32 | c9fdfa27 | Georgios Gousios | from django.conf import settings |
33 | 420f2c20 | Georgios Gousios | |
34 | 420f2c20 | Georgios Gousios | from synnefo.db.models import SynnefoUser |
35 | 420f2c20 | Georgios Gousios | from django.db import transaction |
36 | 420f2c20 | Georgios Gousios | import hashlib |
37 | 938e89ab | Georgios Gousios | import time |
38 | b8033991 | Georgios Gousios | import string |
39 | c9fdfa27 | Georgios Gousios | from datetime import datetime, timedelta |
40 | 420f2c20 | Georgios Gousios | |
41 | 420f2c20 | Georgios Gousios | @transaction.commit_on_success
|
42 | 76cc889c | Georgios Gousios | def _register_user(f, u, unq, t): |
43 | 76cc889c | Georgios Gousios | user = SynnefoUser() |
44 | 76cc889c | Georgios Gousios | user.realname = f |
45 | 76cc889c | Georgios Gousios | user.name = u |
46 | 76cc889c | Georgios Gousios | user.uniq = unq |
47 | 76cc889c | Georgios Gousios | user.type = t |
48 | 76cc889c | Georgios Gousios | user.credit = 10 #TODO: Fix this when we have a per group policy |
49 | 420f2c20 | Georgios Gousios | user.save() |
50 | 938e89ab | Georgios Gousios | create_auth_token(user) |
51 | 420f2c20 | Georgios Gousios | |
52 | 84ae2c4c | Georgios Gousios | def create_uname(fullname): |
53 | 84ae2c4c | Georgios Gousios | fullname = fullname.strip() |
54 | 84ae2c4c | Georgios Gousios | uname = None
|
55 | 84ae2c4c | Georgios Gousios | |
56 | 84ae2c4c | Georgios Gousios | if fullname.find(' ') is not -1: |
57 | b8033991 | Georgios Gousios | (name, surname) = (fullname.split(' ')[0], string.join(fullname.split(' ')[-1:], '')) |
58 | b8033991 | Georgios Gousios | uname = "%s%s" % (string.join(surname[0:7],''), name[0]) |
59 | b8033991 | Georgios Gousios | uname = uname.lower() |
60 | 84ae2c4c | Georgios Gousios | else:
|
61 | 84ae2c4c | Georgios Gousios | uname = fullname[0:7].lower() |
62 | 84ae2c4c | Georgios Gousios | |
63 | 84ae2c4c | Georgios Gousios | return uname
|
64 | 84ae2c4c | Georgios Gousios | |
65 | 76cc889c | Georgios Gousios | @transaction.commit_on_success
|
66 | 76cc889c | Georgios Gousios | def delete_user(user): |
67 | 76cc889c | Georgios Gousios | if user is not None: |
68 | 76cc889c | Georgios Gousios | user.delete() |
69 | 76cc889c | Georgios Gousios | |
70 | 420f2c20 | Georgios Gousios | def register_student(fullname, username, uniqid): |
71 | 420f2c20 | Georgios Gousios | _register_user(fullname, username, uniqid, 'STUDENT')
|
72 | 420f2c20 | Georgios Gousios | |
73 | 420f2c20 | Georgios Gousios | def register_professor(fullname, username, uniqid): |
74 | 420f2c20 | Georgios Gousios | _register_user(fullname, username, uniqid, 'PROFESSOR')
|
75 | 420f2c20 | Georgios Gousios | |
76 | 84ae2c4c | Georgios Gousios | def register_user(fullname, email): |
77 | 84ae2c4c | Georgios Gousios | uname = create_uname (fullname) |
78 | 84ae2c4c | Georgios Gousios | _register_user(fullname, uname, email, 'USER')
|
79 | 84ae2c4c | Georgios Gousios | |
80 | 938e89ab | Georgios Gousios | @transaction.commit_on_success
|
81 | 420f2c20 | Georgios Gousios | def create_auth_token(user): |
82 | 76cc889c | Georgios Gousios | md5 = hashlib.md5() |
83 | 76cc889c | Georgios Gousios | md5.update(user.uniq) |
84 | b8033991 | Georgios Gousios | md5.update(user.name.encode('ascii', 'ignore')) |
85 | 1896d262 | Georgios Gousios | md5.update(time.asctime()) |
86 | 420f2c20 | Georgios Gousios | |
87 | 938e89ab | Georgios Gousios | user.auth_token = md5.hexdigest() |
88 | 1896d262 | Georgios Gousios | user.auth_token_created = datetime.now() |
89 | c9fdfa27 | Georgios Gousios | user.auth_token_expires = user.auth_token_created + \ |
90 | c9fdfa27 | Georgios Gousios | timedelta(hours=settings.AUTH_TOKEN_DURATION) |
91 | 938e89ab | Georgios Gousios | user.save() |
92 | 462c7e47 | Georgios Gousios | |
93 | dd0e2b2d | Georgios Gousios | @transaction.commit_on_success
|
94 | dd0e2b2d | Georgios Gousios | def create_tmp_token(user): |
95 | dd0e2b2d | Georgios Gousios | md5 = hashlib.md5() |
96 | dd0e2b2d | Georgios Gousios | md5.update(user.uniq) |
97 | dd0e2b2d | Georgios Gousios | md5.update(user.name.encode('ascii', 'ignore')) |
98 | dd0e2b2d | Georgios Gousios | md5.update(time.asctime()) |
99 | dd0e2b2d | Georgios Gousios | |
100 | dd0e2b2d | Georgios Gousios | user.tmp_auth_token = md5.hexdigest() |
101 | dd0e2b2d | Georgios Gousios | user.tmp_auth_token_expires = datetime.now() + \ |
102 | dd0e2b2d | Georgios Gousios | timedelta(minutes=settings.HELPDESK_TOKEN_DURATION_MIN) |
103 | dd0e2b2d | Georgios Gousios | user.save() |