| 1 | module main |
| 2 | |
| 3 | import veb |
| 4 | |
| 5 | const admin_users_per_page = 30 |
| 6 | |
| 7 | // TODO move to admin controller |
| 8 | |
| 9 | @['/admin/settings'] |
| 10 | pub fn (mut app App) admin_settings(mut ctx Context) veb.Result { |
| 11 | if !ctx.is_admin() { |
| 12 | return ctx.redirect_to_index() |
| 13 | } |
| 14 | |
| 15 | return $veb.html('templates/admin/settings.html') |
| 16 | } |
| 17 | |
| 18 | @['/admin/settings'; post] |
| 19 | pub fn (mut app App) handle_admin_update_settings(oauth_client_id string, oauth_client_secret string) veb.Result { |
| 20 | if !ctx.is_admin() { |
| 21 | return ctx.redirect_to_index() |
| 22 | } |
| 23 | |
| 24 | tree_folder_size_enabled := 'tree_folder_size_enabled' in ctx.form |
| 25 | app.update_gitly_settings(oauth_client_id, oauth_client_secret, tree_folder_size_enabled) or { |
| 26 | app.info(err.str()) |
| 27 | } |
| 28 | |
| 29 | return ctx.redirect('/admin') |
| 30 | } |
| 31 | |
| 32 | @['/admin/users/:user'; post] |
| 33 | pub fn (mut app App) handle_admin_edit_user(user_id string) veb.Result { |
| 34 | if !ctx.is_admin() { |
| 35 | return ctx.redirect_to_index() |
| 36 | } |
| 37 | |
| 38 | clear_session := 'stop-session' in ctx.form |
| 39 | is_blocked := 'is-blocked' in ctx.form |
| 40 | is_admin := 'is-admin' in ctx.form |
| 41 | |
| 42 | app.edit_user(user_id.int(), clear_session, is_blocked, is_admin) or { app.info(err.str()) } |
| 43 | |
| 44 | return ctx.redirect('/admin') |
| 45 | } |
| 46 | |
| 47 | @['/admin/users'] |
| 48 | pub fn (mut app App) admin_users_default(mut ctx Context) veb.Result { |
| 49 | return app.admin_users(mut ctx, '0') |
| 50 | } |
| 51 | |
| 52 | @['/admin/users/:page'] |
| 53 | pub fn (mut app App) admin_users(mut ctx Context, page string) veb.Result { |
| 54 | if !ctx.is_admin() { |
| 55 | return ctx.redirect_to_index() |
| 56 | } |
| 57 | |
| 58 | page_i := page.int() |
| 59 | user_count := app.get_all_registered_user_count() |
| 60 | offset := admin_users_per_page * page_i |
| 61 | users := app.get_all_registered_users_as_page(offset) |
| 62 | page_count := calculate_pages(user_count, admin_users_per_page) |
| 63 | is_first_page := check_first_page(page_i) |
| 64 | is_last_page := check_last_page(user_count, offset, admin_users_per_page) |
| 65 | prev_page, next_page := generate_prev_next_pages(page_i) |
| 66 | |
| 67 | return $veb.html('templates/admin/users.html') |
| 68 | } |
| 69 | |
| 70 | @['/admin/statistics'] |
| 71 | pub fn (mut app App) admin_statistics() veb.Result { |
| 72 | if !ctx.is_admin() { |
| 73 | return ctx.redirect_to_index() |
| 74 | } |
| 75 | stats := app.get_admin_stats(admin_stats_days) |
| 76 | return $veb.html('templates/admin/statistics.html') |
| 77 | } |
| 78 | |