Statistics
| Branch: | Tag: | Revision:

root / logic / users.py @ 583bfaa0

History | View | Annotate | Download (3.6 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

    
30
# Business Logic for working with users
31

    
32
from django.conf import settings
33

    
34
from synnefo.db.models import SynnefoUser
35
from django.db import transaction
36
import hashlib
37
import time
38
import string
39
from datetime import datetime, timedelta
40

    
41
@transaction.commit_on_success
42
def _register_user(f, u, unq, t):
43
    user = SynnefoUser()
44
    user.realname = f
45
    user.name = u
46
    user.uniq = unq
47
    user.type = t
48
    user.credit = 10 #TODO: Fix this when we have a per group policy
49
    user.save()
50
    create_auth_token(user)
51

    
52
def create_uname(fullname):
53
    fullname = fullname.strip()
54
    uname = None
55

    
56
    if fullname.find(' ') is not -1:
57
        (name, surname) = (fullname.split(' ')[0], string.join(fullname.split(' ')[-1:], ''))
58
        uname = "%s%s" % (string.join(surname[0:7],''), name[0])
59
        uname = uname.lower()
60
    else:
61
        uname = fullname[0:7].lower()
62

    
63
    return uname
64

    
65
@transaction.commit_on_success
66
def delete_user(user):
67
    if user is not None:
68
        user.delete()
69

    
70
def register_student(fullname, username, uniqid):
71
    _register_user(fullname, username, uniqid, 'STUDENT')
72

    
73
def register_professor(fullname, username, uniqid):
74
    _register_user(fullname, username, uniqid, 'PROFESSOR')
75

    
76
def register_user(fullname, email):
77
    uname = create_uname (fullname)
78
    _register_user(fullname, uname, email, 'USER')
79

    
80
@transaction.commit_on_success
81
def create_auth_token(user):
82
    md5 = hashlib.md5()
83
    md5.update(user.uniq)
84
    md5.update(user.name.encode('ascii', 'ignore'))
85
    md5.update(time.asctime())
86

    
87
    user.auth_token = md5.hexdigest()
88
    user.auth_token_created = datetime.now()
89
    user.auth_token_expires = user.auth_token_created + \
90
                              timedelta(hours=settings.AUTH_TOKEN_DURATION)
91
    user.save()
92

    
93
@transaction.commit_on_success
94
def create_tmp_token(user):
95
    md5 = hashlib.md5()
96
    md5.update(user.uniq)
97
    md5.update(user.name.encode('ascii', 'ignore'))
98
    md5.update(time.asctime())
99

    
100
    user.tmp_auth_token = md5.hexdigest()
101
    user.tmp_auth_token_expires = datetime.now() + \
102
                                  timedelta(minutes=settings.HELPDESK_TOKEN_DURATION_MIN)
103
    user.save()