user.py 4.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Copyright (c) 2018, Daniele Venzano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""The User API endpoints."""

import tornado.escape

from zoe_api.rest_api.request_handler import ZoeAPIRequestHandler
21
from zoe_api.exceptions import ZoeException
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36


class UserAPI(ZoeAPIRequestHandler):
    """The User API endpoint. Ops on a single user."""

    def get(self, user_id):
        """HTTP GET method."""
        if self.current_user is None:
            return

        if user_id == self.current_user.id:
            ret = {
                'user': self.current_user.serialize()
            }
        else:
37
38
39
40
            try:
                user = self.api_endpoint.user_by_id(self.current_user, user_id)
            except ZoeException as e:
                self.set_status(e.status_code, e.message)
41
                return
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
            ret = {
                'user': user.serialize()
            }

        self.write(ret)

    def post(self, user_id):
        """HTTP POST method, to edit a user."""
        if self.current_user is None:
            return

        try:
            data = tornado.escape.json_decode(self.request.body)
        except ValueError:
            self.set_status(400, 'Error decoding JSON data')
            return

        try:
60
            self.api_endpoint.user_update(self.current_user, user_id, data)
61
62
63
        except KeyError:
            self.set_status(400, 'Error decoding JSON data')
            return
64
65
        except ZoeException as e:
            self.set_status(e.status_code, e.message)
66
67
68
69
70
71
72
73
74
75
76
            return

        self.set_status(201)

    def delete(self, user_id: int):
        """HTTP DELETE method."""
        if self.current_user is None:
            return

        try:
            self.api_endpoint.user_delete(self.current_user, user_id)
77
78
        except ZoeException as e:
            self.set_status(e.status_code, e.message)
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
            return
        self.set_status(204)


class UserCollectionAPI(ZoeAPIRequestHandler):
    """The UserCollection API. Ops that interact with the User collection."""

    def get(self):
        """HTTP GET method"""
        if self.current_user is None:
            return

        filter_dict = {}

        filters = [
            ('username', str),
            ('email', str),
            ('priority', int),
            ('enabled', bool),
            ('auth_source', str),
            ('role_id', int),
            ('quota_id', int)
        ]
102
103
104
105
        for filt in filters:
            if filt[0] in self.request.arguments:
                if filt[1] == str:
                    filter_dict[filt[0]] = self.request.arguments[filt[0]][0].decode('utf-8')
106
107
108
109
110
111
                if filt[1] == bool:
                    text_val = self.request.arguments[filt[0]][0].decode('utf-8')
                    if text_val == 'False':
                        filter_dict[filt[0]] = False
                    elif text_val == 'True':
                        filter_dict[filt[0]] = True
112
                else:
113
                    filter_dict[filt[0]] = filt[1](self.request.arguments[filt[0]][0])
114
115
116

        try:
            users = self.api_endpoint.user_list(self.current_user, **filter_dict)
117
118
        except ZoeException as e:
            self.set_status(e.status_code, e.message)
119
120
            return

121
        self.write({u.id: u.serialize() for u in users})
122
123
124
125
126
127
128
129
130
131
132
133
134

    def post(self):
        """HTTP POST method."""
        if self.current_user is None:
            return

        try:
            data = tornado.escape.json_decode(self.request.body)
        except ValueError:
            self.set_status(400, 'Error decoding JSON data')
            return

        try:
135
            new_id = self.api_endpoint.user_new(self.current_user, data['username'], data['email'], data['role_id'], data['quota_id'], data['auth_source'], data['fs_uid'])
136
137
138
        except KeyError:
            self.set_status(400, 'Error decoding JSON data')
            return
139
140
        except ZoeException as e:
            self.set_status(e.status_code, e.message)
141
142
143
144
            return

        self.set_status(201)
        self.write({'user_id': new_id})