|
|
|
@ -2,6 +2,8 @@ import logging
|
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
import configparser
|
|
|
|
|
import random
|
|
|
|
|
import string
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from werkzeug.security import check_password_hash
|
|
|
|
|
|
|
|
|
@ -12,6 +14,10 @@ from flask_script import Manager
|
|
|
|
|
from flask_login import LoginManager, login_required, login_user, current_user, logout_user
|
|
|
|
|
from portal.model import db, RegisteredUser
|
|
|
|
|
|
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
|
from email.mime.application import MIMEApplication
|
|
|
|
|
|
|
|
|
|
import portal.solar
|
|
|
|
|
|
|
|
|
|
# TODO: Integrate Shibboleth login.
|
|
|
|
@ -497,6 +503,11 @@ def solar_topuploads_institution(institution_id):
|
|
|
|
|
def solar_uploadstats_institution(institution_id):
|
|
|
|
|
return jsonify(portal.solar.get_institution_upload_stats(institution_id))
|
|
|
|
|
|
|
|
|
|
@app.route(ROUTE_PREFIX + '/uploadstats-per-region')
|
|
|
|
|
@login_required
|
|
|
|
|
def solar_uploadstats_per_region():
|
|
|
|
|
return jsonify(portal.solar.get_region_stats())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route(ROUTE_PREFIX + '/deluser', methods=['POST'])
|
|
|
|
|
@login_required
|
|
|
|
@ -838,6 +849,80 @@ def get_upload_file(upload_id, file_hash):
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
return '', 404
|
|
|
|
|
|
|
|
|
|
@app.route(ROUTE_PREFIX + '/institutionadduser', methods=['POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def solar_institution_add_user():
|
|
|
|
|
|
|
|
|
|
current_user_institution = portal.solar.get_user_institution(current_user.id)
|
|
|
|
|
if not portal.solar.is_institution_coordinator(current_user.id, current_user_institution.id):
|
|
|
|
|
return '', 404
|
|
|
|
|
|
|
|
|
|
name = request.form.get('name')
|
|
|
|
|
email = request.form.get('email')
|
|
|
|
|
role = request.form.get('role')
|
|
|
|
|
password=''.join(random.choices(string.ascii_lowercase, k=8))
|
|
|
|
|
|
|
|
|
|
if not name:
|
|
|
|
|
flash('Prazno polje za ime.')
|
|
|
|
|
return redirect(ROUTE_PREFIX + redirect_url())
|
|
|
|
|
if len(name) > 100:
|
|
|
|
|
flash('Predolgo ime.')
|
|
|
|
|
return redirect(ROUTE_PREFIX + redirect_url())
|
|
|
|
|
|
|
|
|
|
if not email:
|
|
|
|
|
flash('Prazno polje za elektronsko pošto.')
|
|
|
|
|
return redirect(ROUTE_PREFIX + redirect_url())
|
|
|
|
|
if len(email) > 100:
|
|
|
|
|
flash('Predolg email naslov.')
|
|
|
|
|
return redirect(ROUTE_PREFIX + redirect_url())
|
|
|
|
|
elif not re.search(portal.solar.REGEX_EMAIL, email):
|
|
|
|
|
flash('Email napačnega formata.')
|
|
|
|
|
return redirect(ROUTE_PREFIX + redirect_url())
|
|
|
|
|
|
|
|
|
|
if not password:
|
|
|
|
|
flash('Prazno polje za geslo.')
|
|
|
|
|
return redirect(ROUTE_PREFIX + redirect_url())
|
|
|
|
|
if len(password) > 100:
|
|
|
|
|
flash('Predolgo geslo.')
|
|
|
|
|
return redirect(ROUTE_PREFIX + redirect_url())
|
|
|
|
|
|
|
|
|
|
user = portal.solar.get_user_obj_by_email(email)
|
|
|
|
|
if user:
|
|
|
|
|
#portal.solar.undo_remove_user(user.id)
|
|
|
|
|
flash('Uporabnik s tem emailom je že vnešen v sistem.')
|
|
|
|
|
return redirect(ROUTE_PREFIX + redirect_url())
|
|
|
|
|
|
|
|
|
|
new_user_id = portal.solar.register_new_user(name, email, password)
|
|
|
|
|
portal.solar.add_user_to_institution(new_user_id, current_user_institution.id, role)
|
|
|
|
|
portal.solar.activate_user(new_user_id)
|
|
|
|
|
#token za nastaviti geslo
|
|
|
|
|
jwt_token = portal.solar.get_password_reset_token(email, config['APP_SECRET_KEY'])
|
|
|
|
|
|
|
|
|
|
#pošlji email uporabniku
|
|
|
|
|
body = '''
|
|
|
|
|
Ustvarjen je bil uporabniški račun na Portalu Šolar.
|
|
|
|
|
|
|
|
|
|
Geslo lahko nastavite na naslednji povezavi: https://{}/resetpass/{}'''.format(config['SERVER_NAME'], jwt_token)
|
|
|
|
|
message = MIMEMultipart()
|
|
|
|
|
message['From'] = config['MAIL_LOGIN']
|
|
|
|
|
message['To'] = email
|
|
|
|
|
message['Subject'] = 'Portal Šolar: Ponastavitev gesla'
|
|
|
|
|
message.attach(MIMEText(body, "plain"))
|
|
|
|
|
text = message.as_string()
|
|
|
|
|
|
|
|
|
|
# Create a secure SSL context
|
|
|
|
|
context = ssl.create_default_context()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with SMTP_SSL(config['MAIL_HOST'], config['SMTP_PORT'], context=context) as server:
|
|
|
|
|
server.login(config['MAIL_LOGIN'], config['MAIL_PASS'])
|
|
|
|
|
server.sendmail(config['MAIL_LOGIN'], email, text)
|
|
|
|
|
except Exception:
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
flash('Uporabnik je bil uspešno dodan.')
|
|
|
|
|
return redirect(ROUTE_PREFIX + redirect_url())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
app.run(debug=True)
|
|
|
|
|