source: src/mapservice/server_impl/controllers/UsersController.py@ a249e2d

debian/bullseye
Last change on this file since a249e2d was ae2ff15, checked in by Joachim Langenbach <joachim.langenbach@…>, 7 years ago
  • Fixed testing environment
    • Fixed some issues in the API and it's serving
  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[5e4cb73]1'''
[bd0cb38]2 Copyright (C) 2019 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
[5e4cb73]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
[bd0cb38]21import re
22import flask
23from flask import request, render_template
24from flask_mail import Message
[5e4cb73]25
[a88607b]26from .. import mail
[be25db1]27from ..database.Model import User, ApiKey, db
[1cd45dd]28from ..HttpErrors import MapserviceBadRequest, MapserviceUnauthorized, MapserviceNotFound
[5e4cb73]29
[bd0cb38]30mail_regex = re.compile('[^@]+@[^@]+\.[^@]+')
31
[5e4cb73]32def users_activate(activationCode):
33 user = User.query.filter_by(activation_code=activationCode).all()
[bd0cb38]34
35 webclient = flask.current_app.config['MAPSERVICE_WEBCLIENT']
36
[5e4cb73]37 if len(user) != 1:
38 return 'Bad Request', 302, {'Location': 'activation_failed.html'}
39
40 user = user[0]
41 user.activated = True
42 user.activation_code = ""
[bd0cb38]43 db.session.commit()
44 return 'Success', 302, {'Location': '{}/login'.format(webclient)}
[5e4cb73]45
46def users_api_key_create():
[fc99a86]47 user = User.loggedin_user()
[5e4cb73]48 if user is None:
[1cd45dd]49 raise MapserviceUnauthorized
[5e4cb73]50
51 api_key = hashlib.md5("{}{}".format(datetime.datetime.now().strftime("%Y%m%d%H%M%S"), user.salt).encode()).hexdigest()
52 user.apikeys.append(ApiKey(user=user, key=api_key))
[be25db1]53 db.session.commit()
54
55 result = []
56 for apiKey in user.apikeys:
57 result.append(apiKey.key)
58 return result
[5e4cb73]59
60def users_api_key_delete(api_key):
[fc99a86]61 user = User.loggedin_user()
[5e4cb73]62 if user is None:
[1cd45dd]63 raise MapserviceUnauthorized
[5e4cb73]64
[be25db1]65 if not ApiKey.query.filter_by(key=api_key).delete():
[1cd45dd]66 raise MapserviceBadRequest(detail="Could not delete api key {}".format(api_key))
[be25db1]67 db.session.commit()
[5e4cb73]68
69 result = []
70 for apiKey in user.apikeys:
71 result.append(apiKey.key)
72 return result
73
74def users_get():
[fc99a86]75 user = User.loggedin_user()
[5e4cb73]76 if user is None:
[1cd45dd]77 raise MapserviceUnauthorized
[5e4cb73]78
[bd0cb38]79 return user_to_dict(user)
80
81def users_register(user_creation):
82 if user_creation is None:
83 raise MapserviceBadRequest(detail='No data specified')
[a88607b]84 if user_creation.username is None or not user_creation.username:
[bd0cb38]85 raise MapserviceBadRequest(detail='No username given')
[a88607b]86 if user_creation.password is None or not user_creation.password:
[bd0cb38]87 raise MapserviceBadRequest(detail='No password given')
[a88607b]88 if user_creation.first_name is None or not user_creation.first_name:
[bd0cb38]89 raise MapserviceBadRequest(detail='No first name given')
[a88607b]90 if user_creation.last_name is None or not user_creation.last_name:
[bd0cb38]91 raise MapserviceBadRequest(detail='No last name given')
[a88607b]92 if user_creation.email is None or not user_creation.email:
[bd0cb38]93 raise MapserviceBadRequest(detail='No email given')
94 if not mail_regex.match(user_creation.email):
95 raise MapserviceBadRequest(detail='Email contains more than one @, no @ or no .')
96 if user_creation.license_accepted is None or not user_creation.license_accepted:
97 raise MapserviceBadRequest(detail='You must accept the license to register an account')
98
99 # add user to the database
100 user = User.factory(user_creation.username, user_creation.password, user_creation.email)
101 user.first_name = user_creation.first_name
102 user.last_name = user_creation.last_name
103 user.license_accepted = user_creation.license_accepted
104 db.session.add(user)
105 db.session.commit()
106
107 # send activation mail
108 activation_url = '{}/activate/{}'.format(request.base_url, user.activation_code)
109
110 message = Message("Activate your GNUBoat account", recipients=[user.email])
111 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(user.first_name, activation_url)
112 message.html = render_template('activation_mail.html', first_name=user.first_name, activation_url=activation_url)
113 mail.send(message)
114
115 return user_to_dict(user)
116
117def user_to_dict(user):
[5e4cb73]118 # create user dict and remove not needed entries
119 result = user.to_dict()
120 del result['password']
121 del result['salt']
122 del result['activationCode']
123
124 # add api keys of the user to the result dict
125 result['apiKeys'] = []
126 for apiKey in user.apikeys:
127 result['apiKeys'].append(apiKey.key)
128 return result
Note: See TracBrowser for help on using the repository browser.