File size: 4,869 Bytes
1512437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import cherrypy
from various import *

def isProjectValid(con, webName):
    """ Check that the project is an existing one.
    """
    cursor = con.cursor()
    res = False
    if webName is not None :
        query = """
        SELECT id, name
        FROM Project
        WHERE webname = %s"""
        cursor.execute(query, (webName, ))
        rows = cursor.fetchall()
        for row in rows :
            if row[0] > 0 :
                cherrypy.session[str(webName)+'_projectCode'] = row[0]
                cherrypy.session[str(webName)+'_projectName'] = row[1]
                res = True
    return res

def isCategoryValid(category):
    """ Category can only be 'bug' or 'feature'.
    """
    res = False
    if category == 'bug' or category == 'feature' :
        res = True
    return res

def isLoginValid(con, project):
    """ Check that the user is logged for this project.
    Users can be logged on various projects simultaneously
    """
    res = False
    level = cherrypy.session.get(str(project)+'_loginLevel', 0)
    res = (level > 0)
    if res :
        cursor = con.cursor()
        query = """
        SELECT project
        FROM Client
        WHERE id = %s"""
        cursor.execute(query, (cherrypy.session.get(str(project)+'_idUser', -1), ) )
        rows = cursor.fetchall()
        projectID = -2
        for row in rows :
            projectID = row[0]
        if int(projectID) != int(cherrypy.session.get(str(project)+'_projectCode', -1)) :
            res = False
    return res

def isSuperuser(project) :
    """ Check whether the user is superuser (level == 2) or not
    """
    return cherrypy.session.get(str(project)+'_loginLevel', 1) == 2

def isCreator(con, project, category, relativeID, userID):
    """Check whether the is the entry's creator or not"""
    categoryCode = {'bug' : 1, 'feature' : 2}
    cursor = con.cursor()
    query="""SELECT creator FROM Ticket WHERE category=%s AND relativeID=%s"""
    cursor.execute(query, (categoryCode[category], relativeID))
    res = False
    for row in cursor.fetchall():
        if int(row[0]) == int(userID):
            res = True
    return res
    
def isTicketIDValid(con, project, category, ticketID):
    """Check whether ticketID refers to an actuel ticket"""
    categoryCode = {'bug': 1, 'feature': 2}
    cursor = con.cursor()
    query = """
    SELECT count(*) FROM Ticket
    INNER JOIN Project
    ON Project.id = Ticket.project
    WHERE 
        Project.webname = %s
        AND Ticket.category = %s
        AND Ticket.relativeID = %s"""
    cursor.execute(query, (project, categoryCode[category], ticketID))
    res = (int(cursor.fetchone()[0]) == 1)
    return res

def checkProject(con, project) :
    if not isProjectValid(con, project) :
        con.close()
        raise cherrypy.HTTPRedirect( url('new') )

def checkCategory(con, project, category) :
    if not isCategoryValid(category) :
        con.close()
        raise cherrypy.HTTPRedirect( url(project) )

def checkLogin(con, project) :
    if not isLoginValid(con, project) :
        cherrypy.session[str(project)+'_previousPage'] = cherrypy.url()
        con.close()
        raise cherrypy.HTTPRedirect( url(project, 'login') )

def checkSuperUser(con, project) :
    if not isSuperuser(project):
        con.close()
        raise cherrypy.HTTPRedirect( url(project) )


def checkSuperUserOrCreator(con, project, category, ticketID) :
    userID = cherrypy.session.get(str(project)+'_idUser', 0)
    if not isSuperuser(project) and not isCreator(con, project, category, ticketID, userID):
        con.close()
        raise cherrypy.HTTPRedirect( url(project, category, ticketID) )
    
def checkProjectCategoryLogin(con, project, category) :
    """ This is the "normal" checking procedure
    Check that the project is valid, that we are requesting a valid category and that the user is logged in.
    """
    # is the project an existing one ?
    checkProject(con, project)
    # is the category an existing one ?
    checkCategory(con, project, category)
    # is the user logged in ?
    checkLogin(con, project)
    
#def checkProjectCategoryLoginLevel(con, project, category, ticketID) :
#    """ Checks that the user is a superuser in top of the normal procedure
#    """
#    # normal procedure
#    checkProjectCategoryLogin(con, project, category)
#    # is the user a super user or the creator ?
#    checkSuperUserOrCreator(con, project, category, ticketID)

def checkProjectLoginLevel(con, project) :
    # is the project an existing one ?
    checkProject(con, project)
    # is the user logged in ?
    checkLogin(con, project)
    # is the user a super user ?
    checkSuperUser(con, project)
    
def checkProjectLogin(con, project) :
    # is the project an existing one ?
    checkProject(con, project)
    # is the user logged in ?
    checkLogin(con, project)