| 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 | '''
|
|---|
| 17 | import connexion
|
|---|
| 18 |
|
|---|
| 19 | import datetime
|
|---|
| 20 | import hashlib
|
|---|
| 21 | import re
|
|---|
| 22 | import flask
|
|---|
| 23 | from flask import request, render_template
|
|---|
| 24 | from flask_mail import Message
|
|---|
| 25 |
|
|---|
| 26 | from .. import mail
|
|---|
| 27 | from ..database.Model import User, ApiKey, db
|
|---|
| 28 | from ..HttpErrors import MapserviceBadRequest, MapserviceUnauthorized, MapserviceNotFound
|
|---|
| 29 |
|
|---|
| 30 | mail_regex = re.compile('[^@]+@[^@]+\.[^@]+')
|
|---|
| 31 |
|
|---|
| 32 | def users_activate(activationCode):
|
|---|
| 33 | user = User.query.filter_by(activation_code=activationCode).all()
|
|---|
| 34 |
|
|---|
| 35 | webclient = flask.current_app.config['MAPSERVICE_WEBCLIENT']
|
|---|
| 36 |
|
|---|
| 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 = ""
|
|---|
| 43 | db.session.commit()
|
|---|
| 44 | return 'Success', 302, {'Location': '{}/login'.format(webclient)}
|
|---|
| 45 |
|
|---|
| 46 | def users_api_key_create():
|
|---|
| 47 | user = User.loggedin_user()
|
|---|
| 48 | if user is None:
|
|---|
| 49 | raise MapserviceUnauthorized
|
|---|
| 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))
|
|---|
| 53 | db.session.commit()
|
|---|
| 54 |
|
|---|
| 55 | result = []
|
|---|
| 56 | for apiKey in user.apikeys:
|
|---|
| 57 | result.append(apiKey.key)
|
|---|
| 58 | return result
|
|---|
| 59 |
|
|---|
| 60 | def users_api_key_delete(api_key):
|
|---|
| 61 | user = User.loggedin_user()
|
|---|
| 62 | if user is None:
|
|---|
| 63 | raise MapserviceUnauthorized
|
|---|
| 64 |
|
|---|
| 65 | if not ApiKey.query.filter_by(key=api_key).delete():
|
|---|
| 66 | raise MapserviceBadRequest(detail="Could not delete api key {}".format(api_key))
|
|---|
| 67 | db.session.commit()
|
|---|
| 68 |
|
|---|
| 69 | result = []
|
|---|
| 70 | for apiKey in user.apikeys:
|
|---|
| 71 | result.append(apiKey.key)
|
|---|
| 72 | return result
|
|---|
| 73 |
|
|---|
| 74 | def users_get():
|
|---|
| 75 | user = User.loggedin_user()
|
|---|
| 76 | if user is None:
|
|---|
| 77 | raise MapserviceUnauthorized
|
|---|
| 78 |
|
|---|
| 79 | return user_to_dict(user)
|
|---|
| 80 |
|
|---|
| 81 | def users_register(user_creation):
|
|---|
| 82 | if user_creation is None:
|
|---|
| 83 | raise MapserviceBadRequest(detail='No data specified')
|
|---|
| 84 | if user_creation.username is None or not user_creation.username:
|
|---|
| 85 | raise MapserviceBadRequest(detail='No username given')
|
|---|
| 86 | if user_creation.password is None or not user_creation.password:
|
|---|
| 87 | raise MapserviceBadRequest(detail='No password given')
|
|---|
| 88 | if user_creation.first_name is None or not user_creation.first_name:
|
|---|
| 89 | raise MapserviceBadRequest(detail='No first name given')
|
|---|
| 90 | if user_creation.last_name is None or not user_creation.last_name:
|
|---|
| 91 | raise MapserviceBadRequest(detail='No last name given')
|
|---|
| 92 | if user_creation.email is None or not user_creation.email:
|
|---|
| 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 |
|
|---|
| 117 | def user_to_dict(user):
|
|---|
| 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
|
|---|