Esempio n. 1
0
def login():
    try:
        if session['email'] is not None:
            return redirect('/workspace')
    except:
        pass

    try:
        form = LoginForm()
        if form.validate_on_submit():
            dbase = './troops-to-task/user_db.sqlite'
            sql = t2t_sqlite.getUser(form.email.data)
            check = callSQL(dbase, sql)

            if bcrypt.hashpw(form.pwd.data, check[0][4]) == check[0][4]:
                debug('User Logged In! ')
                session['remember_me'] = form.remember_me.data
                session['email'] = check[0][3]
                return redirect('/workspace')
            else:
                flash('Your username or password was not correct. :(')

        return render_template('login.html',
                               title='Sign In',
                               form=form)
    except:
        return show_error_page('Error during login route')
def hash_password(password, salt):
    encoded = password.encode('utf-8')
    return bcrypt.hashpw(encoded, salt)
Esempio n. 3
0
def new():
        form = NewAccount()
        if form.validate_on_submit():
            check_userdb()
            pwdHash = bcrypt.hashpw(form.pwd.data, bcrypt.gensalt())
            usersDB = bcrypt.hashpw(form.first_name.data + form.last_name.data + form.email.data, bcrypt.gensalt())
            dbase = './troops-to-task/user_db.sqlite'
            try:
                sql = t2t_sqlite.addUser(form.first_name.data, form.last_name.data, form.email.data, pwdHash, usersDB)
                debug(sql)
            except:
                flash('Error in generating SQL for new account.')
            try:
                theReturn = callSQL(dbase, sql)
                debug(theReturn)
            except:
                flash('Error Calling SQL: ' + sql)


            sql = t2t_sqlite.getUser(form.email.data)
            check = callSQL(dbase, sql)

            if bcrypt.hashpw(form.pwd.data, check[0][4]) == check[0][4]:
                usersdb = './troops-to-task/userdb/' + check[0][5] + '.sqlite'
                init_sql = '''
CREATE TABLE troops (
    troopid char(10) NOT NULL PRIMARY KEY,
    rank char(5) NOT NULL,
    first_name char(50),
    last_name char(50) NOT NULL,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);

CREATE TABLE tasks (
    taskid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    troop_id INTEGER NOT NULL,
    event_id INTEGER NOt NULL,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(troop_id) REFERENCES troops(dodid),
    FOREIGN KEY(event_id) REFERENCES events(eventid));

CREATE TABLE log (
    logid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    event text,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);

CREATE TABLE events (
    eventid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    title char(50) NOT NULL,
    startDTG DATETIME NOT NULL,
    endDTG DATETIME NOT NULL,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);
'''

                try:
                    connect_db(usersdb)      # SQLite3 auto-creates on connect if it does not exists
                except:
                    flash('Error connecting to user''s database.')
                try:
                    cursor.executescript(init_sql)               # initialize
                except:
                    flash('Error creating user''s database.')
                try:
                    disconnect_db()
                except:
                    flash('Error disconnecting from user''s database.')

                flash('Account created successfully!')
                #account created, proceed to login the user
            else:
                flash('Sorry, there was an error creating your account. :(')
            return redirect('/index')
        return render_template('new.html',
                               title='Sign Up!',
                               form=form)