source: src/mapservice/server_impl/controllers/UsersController.py@ 8818b65

debian/bullseye
Last change on this file since 8818b65 was 7be94aa, checked in by Joachim Langenbach <joachim.langenbach@…>, 7 years ago
  • Added yapf style config (src/python-style.yapf)
    • Updated all files to the yapf formatted version
  • Property mode set to 100644
File size: 5.0 KB
Line 
1'''
2 Copyright (C) 2019 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16'''
17import connexion
18
19import datetime
20import hashlib
21import re
22import flask
23from flask import request, render_template
24from flask_mail import Message
25
26from .. import mail
27from ..database.Model import User, ApiKey, db
28from ..HttpErrors import MapserviceBadRequest, MapserviceUnauthorized, MapserviceNotFound
29
30mail_regex = re.compile('[^@]+@[^@]+\.[^@]+')
31
32
33def users_activate(activationCode):
34 user = User.query.filter_by(activation_code=activationCode).all()
35
36 webclient = flask.current_app.config['MAPSERVICE_WEBCLIENT']
37
38 if len(user) != 1:
39 return 'Bad Request', 302, {'Location': 'activation_failed.html'}
40
41 user = user[0]
42 user.activated = True
43 user.activation_code = ""
44 db.session.commit()
45 return 'Success', 302, {'Location': '{}/login'.format(webclient)}
46
47
48def users_api_key_create():
49 user = User.loggedin_user()
50 if user is None:
51 raise MapserviceUnauthorized
52
53 api_key = hashlib.md5(
54 "{}{}".format(
55 datetime.datetime.now().strftime("%Y%m%d%H%M%S"), user.salt
56 ).encode()
57 ).hexdigest()
58 user.apikeys.append(ApiKey(user=user, key=api_key))
59 db.session.commit()
60
61 result = []
62 for apiKey in user.apikeys:
63 result.append(apiKey.key)
64 return result
65
66
67def users_api_key_delete(api_key):
68 user = User.loggedin_user()
69 if user is None:
70 raise MapserviceUnauthorized
71
72 if not ApiKey.query.filter_by(key=api_key).delete():
73 raise MapserviceBadRequest(
74 detail="Could not delete api key {}".format(api_key)
75 )
76 db.session.commit()
77
78 result = []
79 for apiKey in user.apikeys:
80 result.append(apiKey.key)
81 return result
82
83
84def users_get():
85 user = User.loggedin_user()
86 if user is None:
87 raise MapserviceUnauthorized
88
89 return user_to_dict(user)
90
91
92def users_register(user_creation):
93 if user_creation is None:
94 raise MapserviceBadRequest(detail='No data specified')
95 if user_creation.username is None or not user_creation.username:
96 raise MapserviceBadRequest(detail='No username given')
97 if user_creation.password is None or not user_creation.password:
98 raise MapserviceBadRequest(detail='No password given')
99 if user_creation.first_name is None or not user_creation.first_name:
100 raise MapserviceBadRequest(detail='No first name given')
101 if user_creation.last_name is None or not user_creation.last_name:
102 raise MapserviceBadRequest(detail='No last name given')
103 if user_creation.email is None or not user_creation.email:
104 raise MapserviceBadRequest(detail='No email given')
105 if not mail_regex.match(user_creation.email):
106 raise MapserviceBadRequest(
107 detail='Email contains more than one @, no @ or no .'
108 )
109 if user_creation.license_accepted is None or not user_creation.license_accepted:
110 raise MapserviceBadRequest(
111 detail='You must accept the license to register an account'
112 )
113
114 # add user to the database
115 user = User.factory(
116 user_creation.username, user_creation.password, user_creation.email
117 )
118 user.first_name = user_creation.first_name
119 user.last_name = user_creation.last_name
120 user.license_accepted = user_creation.license_accepted
121 db.session.add(user)
122 db.session.commit()
123
124 # send activation mail
125 activation_url = '{}/activate/{}'.format(
126 request.base_url, user.activation_code
127 )
128
129 message = Message("Activate your GNUBoat account", recipients=[user.email])
130 message.body = 'Hello {},\nthank you for support the project with your own measurings. To activate your GNUBoat account, visit {}.\nKind regards,\nYour GNUBoat team'.format(
131 user.first_name, activation_url
132 )
133 message.html = render_template(
134 'activation_mail.html',
135 first_name=user.first_name,
136 activation_url=activation_url
137 )
138 mail.send(message)
139
140 return user_to_dict(user)
141
142
143def user_to_dict(user):
144 # create user dict and remove not needed entries
145 result = user.to_dict()
146 del result['password']
147 del result['salt']
148 del result['activationCode']
149
150 # add api keys of the user to the result dict
151 result['apiKeys'] = []
152 for apiKey in user.apikeys:
153 result['apiKeys'].append(apiKey.key)
154 return result
Note: See TracBrowser for help on using the repository browser.