config.py 7.87 KB
Newer Older
Marc Duez's avatar
Marc Duez committed
1
# coding: utf8
Marc Duez's avatar
Marc Duez committed
2
import gluon.contrib.simplejson
3
from controller_utils import error_message
Marc Duez's avatar
Marc Duez committed
4
5
6
7
if request.env.http_origin:
    response.headers['Access-Control-Allow-Origin'] = request.env.http_origin  
    response.headers['Access-Control-Allow-Credentials'] = 'true'
    response.headers['Access-Control-Max-Age'] = 86400
Marc Duez's avatar
Marc Duez committed
8

Marc Duez's avatar
Marc Duez committed
9
10
11
    
ACCESS_DENIED = "access denied"

Marc Duez's avatar
Marc Duez committed
12
def index():
Marc Duez's avatar
Marc Duez committed
13
14
15
    if not auth.user : 
        res = {"redirect" : "default/user/login"}
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
16
17
    

18
    query = db((auth.accessible_query('read', db.config) | auth.accessible_query('admin', db.config) ) ).select(orderby=~db.config.name)
Marc Duez's avatar
Marc Duez committed
19
20

    return dict(message=T('Configs'),
21
               query=query,
22
               isAdmin = auth.is_admin())
Marc Duez's avatar
Marc Duez committed
23

24

Marc Duez's avatar
Marc Duez committed
25
def add(): 
Mathieu Giraud's avatar
Mathieu Giraud committed
26
    return dict(message=T('Add config'))
Marc Duez's avatar
Marc Duez committed
27

28

Marc Duez's avatar
Marc Duez committed
29
30
#TODO check data
def add_form(): 
31
32
    error =""

33
    required_fields = ['config_name', 'config_command', 'config_fuse_command', 'config_program']
34
35
36
    for field in required_fields:
        if request.vars[field] == "" :
            error += field+" needed, "
37
38
39

    if error=="" :
        
40
        config_id = db.config.insert(name=request.vars['config_name'],
41
42
                        info=request.vars['config_info'],
                        command=request.vars['config_command'],
43
44
                        fuse_command=request.vars['config_fuse_command'],
                        program=request.vars['config_program']
45
46
                        )

47
48
49
50
51
52
53
54
55
56
57
58
        user_group = None
        group_ids = list(auth.user_groups.keys())
        for gid in group_ids:
            if (auth.user_groups[gid] != 'public'):
                user_group = gid
                break

        db.auth_permission.insert(group_id=user_group,
                                name='create',
                                table_name='config',
                                record_id=config_id)

Marc Duez's avatar
Marc Duez committed
59
        res = {"redirect": "config/index",
60
               "message": "config '%s' added" % request.vars['config_name']}
61
        log.admin(res)
62
63
64
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
        
    else :
Marc Duez's avatar
Marc Duez committed
65
        res = {"success" : "false", "message" : error}
66
        log.error(res)
67
68
69
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))


70
71
72
73
def edit():
    if (auth.can_modify_config(request.vars['config_id'])):
        return dict(message=T('edit config'))
    return error_message(ACCESS_DENIED)
74
75
76
77
78


def edit_form(): 
    error =""

Ryan Herbert's avatar
Ryan Herbert committed
79
    if (not auth.can_modify_config(request.vars['config_id'])):
80
81
        error += "ACCESS_DENIED"

Marc Duez's avatar
Marc Duez committed
82
    required_fields = ['id', 'config_name', 'config_command', 'config_fuse_command', 'config_program']
83
84
85
    for field in required_fields:
        if request.vars[field] == "" :
            error += field+" needed, "
86
87
88
89

    if error=="" :

        db.config[request.vars["id"]] = dict(name=request.vars['config_name'],
90
91
92
93
94
                                             info=request.vars['config_info'],
                                             command=request.vars['config_command'],
                                             fuse_command=request.vars['config_fuse_command'],
                                             program=request.vars['config_program']
                                             )
95

Marc Duez's avatar
Marc Duez committed
96
        res = {"redirect": "config/index",
97
               "message": "config '%s' updated" % request.vars['config_name']}
Marc Duez's avatar
Marc Duez committed
98

99
        log.admin(res)
100
101
102
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))

    else :
Marc Duez's avatar
Marc Duez committed
103
        res = {"success" : "false", "message" : error}
104
        log.error(res)
105
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
106
107

def confirm():
108
109
110
    if (auth.can_modify_config(request.vars['id'])):
        return dict(message=T('confirm config deletion'))
    return error_message(ACCESS_DENIED)
111
112

def delete():
113
114
115
116
117
118
119
120
121
122
123
124
    if (auth.can_modify_config(request.vars['id'])):
        #delete results_file using this config
        db(db.results_file.config_id==request.vars["id"]).delete()

        #delete config
        db(db.config.id==request.vars["id"]).delete()

        res = {"redirect": "config/index",
               "message": "config '%s' deleted" % request.vars["id"]}
        log.admin(res)
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
    return error_message(ACCESS_DENIED)
125
126
127


def permission(): 
128
    if (auth.can_modify_patient(request.vars["id"]) ):
129
        
130
131
132
133
134
135
136
137
138
139
140
        query = db( (db.auth_group.role != 'admin') ).select()
        
        query2 = db( (db.auth_group.role != 'admin') &
                    (db.auth_membership.group_id == db.auth_group.id) &
                    (db.auth_membership.user_id == db.auth_user.id)    
                  ).select()
        
        usermap = {}
        for row in query2 : 
            if row.auth_group.role[:5] == "user_" :
                usermap[row.auth_group.role] = row.auth_user.id 
141
142
143
144
        
        for row in query :
            row.owner = row.role
            if row.owner[:5] == "user_" :
145
                id = usermap[row.owner]
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
                row.owner = db.auth_user[id].first_name + " " + db.auth_user[id].last_name 

            row.admin = False
            if db(   (db.auth_permission.name == "admin")
                  & (db.auth_permission.record_id == request.vars["id"])
                  & (db.auth_permission.group_id == row.id)
                  & (db.auth_permission.table_name == db.config)
              ).count() > 0 :
                row.admin = True
                
            row.read = False
            if db(   (db.auth_permission.name == "read")
                  & (db.auth_permission.record_id == request.vars["id"])
                  & (db.auth_permission.group_id == row.id)
                  & (db.auth_permission.table_name == db.config)
              ).count() > 0 :
                row.read = True
        
        return dict(query = query)
    else :
        res = {"message": ACCESS_DENIED}
        log.error(res)
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
    
#TODO refactor with patient/change_permission
def change_permission():
172
    if (auth.can_modify_config(request.vars["config_id"]) ):
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
        error = ""
        if request.vars["group_id"] == "" :
            error += "missing group_id, "
        if request.vars["config_id"] == "" :
            error += "missing patient_id, "
        if request.vars["permission"] == "" :
            error += "missing permission, "

        if error=="":
            if db(   (db.auth_permission.name == request.vars["permission"])
                      & (db.auth_permission.record_id == request.vars["config_id"])
                      & (db.auth_permission.group_id == request.vars["group_id"])
                      & (db.auth_permission.table_name == db.config)
                  ).count() > 0 :
                auth.del_permission(request.vars["group_id"], request.vars["permission"], db.config, request.vars["config_id"])
188
189
                res = {"message" : "c%s: access '%s' deleted to '%s'" % (request.vars["config_id"],
                                                                         request.vars["permission"], db.auth_group[request.vars["group_id"]].role)}
190
191
            else :
                auth.add_permission(request.vars["group_id"], request.vars["permission"], db.config, request.vars["config_id"])
192
193
                res = {"message" : "c%s: access '%s' granted to '%s'" % (request.vars["config_id"],
                                                                         request.vars["permission"], db.auth_group[request.vars["group_id"]].role)}
194
            
195
            log.admin(res)
196
197
198
199
200
201
202
203
204
            return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
        else :
            res = {"message": "incomplete request : "+error }
            log.error(res)
            return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
    else :
        res = {"message": ACCESS_DENIED}
        log.error(res)
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))