| 1 | module main |
| 2 | |
| 3 | import veb |
| 4 | import validation |
| 5 | import api |
| 6 | |
| 7 | @['/:username/settings/ssh-keys'] |
| 8 | pub fn (mut app App) user_ssh_keys_list(mut ctx Context, username string) veb.Result { |
| 9 | is_users_settings := username == ctx.user.username |
| 10 | |
| 11 | if !ctx.logged_in || !is_users_settings { |
| 12 | return ctx.redirect_to_index() |
| 13 | } |
| 14 | |
| 15 | ssh_keys := app.find_ssh_keys(ctx.user.id) |
| 16 | |
| 17 | return $veb.html('templates/user/ssh/keys/list.html') |
| 18 | } |
| 19 | |
| 20 | @['/:username/settings/ssh-keys'; 'post'] |
| 21 | pub fn (mut app App) handle_add_ssh_key(mut ctx Context, username string) veb.Result { |
| 22 | is_users_settings := username == ctx.user.username |
| 23 | |
| 24 | if !ctx.logged_in || !is_users_settings { |
| 25 | return ctx.redirect_to_index() |
| 26 | } |
| 27 | |
| 28 | title := ctx.form['title'] |
| 29 | ssh_key := ctx.form['key'] |
| 30 | |
| 31 | is_title_empty := validation.is_string_empty(title) |
| 32 | is_ssh_key_empty := validation.is_string_empty(ssh_key) |
| 33 | |
| 34 | if is_title_empty { |
| 35 | ctx.error('Title is empty') |
| 36 | |
| 37 | return app.user_ssh_keys_new(mut ctx, username) |
| 38 | } |
| 39 | |
| 40 | if is_ssh_key_empty { |
| 41 | ctx.error('SSH key is empty') |
| 42 | |
| 43 | return app.user_ssh_keys_new(mut ctx, username) |
| 44 | } |
| 45 | |
| 46 | app.add_ssh_key(ctx.user.id, title, ssh_key) or { |
| 47 | ctx.error(err.str()) |
| 48 | |
| 49 | return app.user_ssh_keys_new(mut ctx, username) |
| 50 | } |
| 51 | |
| 52 | return ctx.redirect('/${username}/settings/ssh-keys') |
| 53 | } |
| 54 | |
| 55 | @['/:username/settings/ssh-keys/:id'; 'delete'] |
| 56 | pub fn (mut app App) handle_remove_ssh_key(mut ctx Context, username string, id string) veb.Result { |
| 57 | is_users_settings := username == ctx.user.username |
| 58 | |
| 59 | if !ctx.logged_in || !is_users_settings { |
| 60 | return ctx.redirect_to_index() |
| 61 | } |
| 62 | |
| 63 | app.remove_ssh_key(ctx.user.id, id.int()) or { |
| 64 | response := api.ApiErrorResponse{ |
| 65 | message: 'There was an error while deleting the SSH key' |
| 66 | } |
| 67 | |
| 68 | return ctx.json(response) |
| 69 | } |
| 70 | |
| 71 | return ctx.ok('') |
| 72 | } |
| 73 | |
| 74 | @['/:username/settings/ssh-keys/new'] |
| 75 | pub fn (mut app App) user_ssh_keys_new(mut ctx Context, username string) veb.Result { |
| 76 | is_users_settings := username == ctx.user.username |
| 77 | |
| 78 | if !ctx.logged_in || !is_users_settings { |
| 79 | return ctx.redirect_to_index() |
| 80 | } |
| 81 | |
| 82 | return $veb.html('templates/user/ssh/keys/new.html') |
| 83 | } |
| 84 | |