160 lines
7.8 KiB
Python
160 lines
7.8 KiB
Python
from flask import Blueprint, Flask, request, flash, render_template, url_for, session, redirect, abort, make_response, send_file
|
|
from flask_bcrypt import Bcrypt
|
|
import time
|
|
from markupsafe import escape
|
|
import sqlite3
|
|
import glob, os, sys, time
|
|
from tools.utils import email_disp, valid_token_register, valid_passwd, valid_username
|
|
from socket import gethostname
|
|
import subprocess
|
|
from tools.filesutils import check_and_create
|
|
from tools.utils import set_mail_domain
|
|
app = Flask( 'pywallter' )
|
|
app.config.from_pyfile('config.py')
|
|
bcrypt = Bcrypt(app)
|
|
|
|
#### Variables ##################################################################################
|
|
|
|
DATAS_USER = app.config['DOSSIER_APP']
|
|
DOSSIER_PERSO = app.config['DOSSIER_APP']
|
|
extensionimg = app.config['EXT_IMG']
|
|
DATABASE = app.config['DATABASE']
|
|
MAIL_SERVER = app.config['MAIL_SERVER']
|
|
XMPP_SERVER = app.config['XMPP_SERVER']
|
|
SETUID = app.config['SETUID']
|
|
BASE_URL = app.config['BASE_URL']
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
inscription = Blueprint('inscription', __name__, template_folder='templates')
|
|
|
|
@inscription.route( '/inscription/<token>', methods=['GET','POST'] )
|
|
def signin(token) :
|
|
|
|
mail_domain = set_mail_domain()
|
|
|
|
url_inscription = BASE_URL +'/inscription/'+token
|
|
resp = None
|
|
if valid_token_register(token, "Invitation"):
|
|
if 'username' in session :
|
|
resp = redirect(url_for('profil.profile', _external=True))
|
|
else :
|
|
|
|
# Réponse si la requete est de type GET ou si la requete POST echoue
|
|
resp = render_template('inscription.html',
|
|
signin_enable=app.config['SIGNIN_ENABLE'],
|
|
token=token, hostname=mail_domain,
|
|
url_inscription=url_inscription,
|
|
MAIL_SERVER=MAIL_SERVER, XMPP_SERVER=XMPP_SERVER)
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
#On test si aucun champs du formulaire n'est vide.
|
|
if len(request.form['user']) == 0 or \
|
|
len(request.form['passwd']) == 0 or \
|
|
len(request.form['passwdconfirm']) == 0:
|
|
|
|
flash(u'Il faut remplir le formulaire en entier, les champs ne peuvent pas etre vide ', 'error')
|
|
return render_template('inscription.html',
|
|
signin_enable=app.config['SIGNIN_ENABLE'],
|
|
token=token, hostname=hostname,
|
|
url_inscription=url_inscription,
|
|
MAIL_SERVER=MAIL_SERVER, XMPP_SERVER=XMPP_SERVER)
|
|
|
|
user = str(request.form['user'])
|
|
passwd = str(request.form['passwd'])
|
|
passwdconfirm = str(request.form['passwdconfirm'])
|
|
|
|
bcrypt_passwd = bcrypt.generate_password_hash(passwd)
|
|
mail_passwd_change = 0
|
|
|
|
not_error = True
|
|
|
|
conn = sqlite3.connect(DATABASE) # Connexion à la base de donnée
|
|
cursor = conn.cursor() # Création de l'objet "curseur"
|
|
|
|
cursor.execute("""SELECT name FROM users WHERE name=?""", (user,))
|
|
testuser = cursor.fetchone()
|
|
conn.close()
|
|
|
|
print("user :" +user)
|
|
print ("passwd: "+ passwd)
|
|
if testuser and valid_username(user):
|
|
flash(u'Le Nom d\'utilisateur déjà utilisé ou contient des caractères invalides, merci d\'en choisir un autre', 'error')
|
|
flash(u'Les caractères espaces et \' ( ) < > , ; : " [ ] | ç % & ne sont pas autorisés', 'error')
|
|
not_error = False
|
|
|
|
|
|
if MAIL_SERVER:
|
|
mail = user.lower()+'@'+hostname
|
|
if not(email_disp(mail)) :
|
|
flash(u'Cette Adresse email est déjà utilisée , merci d\'en choisir une autre', 'error')
|
|
not_error = False
|
|
|
|
if not_error:
|
|
confirmation = bcrypt.check_password_hash(bcrypt_passwd, passwdconfirm)
|
|
if confirmation is False:
|
|
flash(u'Les mots de passe ne sont pas identiques !', 'error')
|
|
else:
|
|
conn = sqlite3.connect(DATABASE) # Connexion à la base de donnée
|
|
cursor = conn.cursor() # Création de l'objet "curseur"
|
|
cursor.execute("""INSERT INTO users(name, passwd) VALUES(?, ?)""", (user, bcrypt_passwd)) # Insérer des valeurs
|
|
conn.commit() # Sauvegarder valeurs dans la bdd
|
|
|
|
# On change le mot de passe du compte mail
|
|
if MAIL_SERVER:
|
|
cmd = subprocess.run([SETUID, 'set_mail_passwd', mail, passwd],stdout=subprocess.PIPE)
|
|
if cmd.returncode != 0:
|
|
flash(u'Il y a eu une problème lors du changement de mot passe pour le compte Mail', 'error')
|
|
else:
|
|
cursor.execute("UPDATE users SET mail=? WHERE name=?", (mail, user))
|
|
|
|
# On change le mot de passe du compte XMPP
|
|
if XMPP_SERVER:
|
|
jid = mail.split('@')
|
|
cmd = subprocess.run([SETUID,'prosodyctl', 'register', jid[0],jid[1],passwd],stdout=subprocess.PIPE)
|
|
|
|
if cmd.returncode != 0:
|
|
flash(u'Il y a eu un problème pour la création du compte XMPP !', 'error')
|
|
|
|
check_and_create(DOSSIER_PERSO + user)
|
|
time_create_user = time.strftime("%A %d %B %Y %H:%M:%S")
|
|
ip_address=request.environ['REMOTE_ADDR']
|
|
client_plateform=request.headers.get('User-Agent')
|
|
|
|
log=time_create_user + ' - ' + ip_address + ' - ' + user + ' - ' + client_plateform + '\n' + '---> ' + 'Création du compte \n'
|
|
log_file=DOSSIER_PERSO+user+"/log.txt"
|
|
try:
|
|
with open(log_file, 'x') as file:
|
|
file.write(log)
|
|
except FileExistsError:
|
|
print('The file '+log_file +' already exists')
|
|
|
|
# Une fois que tout c'est bien passé pour l'inscription on détruit le jeton.
|
|
cursor.execute("""SELECT name, invitations FROM users where Token=?""", (token,))
|
|
tmp = cursor.fetchone()
|
|
username = tmp[0]
|
|
invitations_count=tmp[1] - 1
|
|
if username == "pywallter":
|
|
cursor.execute("""DELETE from users where name = ?""", (username,))
|
|
else:
|
|
cursor.execute("""UPDATE users set invitations=?, Token='' where name=?""", (invitations_count, username,))
|
|
conn.commit()
|
|
|
|
flash(u'Inscription réalisée avec succés !', 'success')
|
|
resp = redirect(url_for('loginlogout.login'))
|
|
else:
|
|
return render_template('inscription.html',
|
|
signin_enable=app.config['SIGNIN_ENABLE'],
|
|
token=token, hostname=hostname,
|
|
url_inscription=url_inscription,
|
|
MAIL_SERVER=MAIL_SERVER, XMPP_SERVER=XMPP_SERVER)
|
|
else:
|
|
resp = redirect(BASE_URL, code=401)
|
|
|
|
return resp
|
|
|