fix "user undefined"

This commit is contained in:
Jakobus Schürz 2019-09-29 22:45:00 +02:00
parent eecd89ac49
commit 0dd814b33e
3 changed files with 13 additions and 5 deletions

View file

@ -54,6 +54,10 @@ def get_pubkey(id, check_user=True):
@bp.route('/create', methods=('GET', 'POST'))
@login_required
def create():
db = get_db()
user = db.execute(
'SELECT * FROM user WHERE id = ?', (g.user['id'],)
).fetchone()
if request.method == 'POST':
key_name = request.form['key_name']
ssh_pubkey = request.form['ssh_pubkey']
@ -64,11 +68,11 @@ def create():
fingerprint = str(base64.b64encode(hashlib.sha256(base64.b64decode(key)).digest()), "utf-8").rstrip('=')
print(fingerprint)
db = get_db()
ckfp = db.execute(
'SELECT id, fingerprint, deleted FROM pubkeys WHERE fingerprint = ?', (fingerprint, )
).fetchone()
if ckfp != None:
if ckfp['fingerprint'] == fingerprint:
if ckfp['deleted'] == 0:
@ -96,13 +100,17 @@ def create():
db.commit()
return redirect(url_for('pubkeys.index'))
return render_template('pubkeys/create.html')
return render_template('pubkeys/create.html', user=user)
@bp.route('/<int:id>/update', methods=('GET', 'POST'))
@login_required
def update(id):
pubkey = get_pubkey(id)
db = get_db()
user = db.execute(
'SELECT * FROM user WHERE id = ?', (g.user['id'],)
).fetchone()
if request.method == 'POST':
key_name = request.form['key_name']
deleted = request.form.get('deleted')
@ -118,7 +126,6 @@ def update(id):
if error is not None:
flash(error)
else:
db = get_db()
db.execute(
'UPDATE pubkeys SET key_name = ?, deleted = ?'
' WHERE id = ?',
@ -127,7 +134,7 @@ def update(id):
db.commit()
return redirect(url_for('pubkeys.index'))
return render_template('pubkeys/update.html', pubkey=pubkey)
return render_template('pubkeys/update.html', pubkey=pubkey, user=user)
@bp.route('/<int:id>/delete', methods=('GET',))
@login_required

View file

@ -29,3 +29,4 @@ nav ul li a, nav ul li span, header .action { display: block; padding: 0.5rem; }
.danger { color: #cc2f2e; }
input.danger { color: #cc2f2e; }
input[type=submit] { align-self: start; min-width: 10em; }
.key { color: #cc22ff; }

View file

@ -45,7 +45,7 @@
{% endif %}
<div name="fingerprint" id="fingerprint">fingerprint: {{ pubkey['fingerprint'] }}</div>
<br>
<div name="ssh_pubkey" id="ssh_pubkey">{{ request.form['ssh_pubkey'] or pubkey['ssh_pubkey'] }}</div>
<div class="key" name="ssh_pubkey" id="ssh_pubkey">{{ request.form['ssh_pubkey'] or pubkey['ssh_pubkey'] }}</div>
<p class="about">registered on {{ pubkey['created'].strftime('%Y-%m-%d') }}</p>
</form>
<!--form action="{{ url_for('pubkeys.revoke', id=pubkey['id']) }}" method="POST">