| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 10 | "gitlab.com/Timfa/bluegit/internal/store" |
| 11 | ) |
| 12 | |
| 13 | // Beheer > Gebruikers: elke sitebeheerder kan een ander beheerder maken of |
| 14 | // die rechten weer afnemen. Jezelf degraderen kan niet, en er blijft altijd |
| 15 | // minstens één beheerder (store.SetInstanceAdmin). Elke wijziging komt als |
| 16 | // auditregel in de log ("admin audit"). |
| 17 | |
| 18 | // AdminTab is één subtab onder Beheer. |
| 19 | type AdminTab struct { |
| 20 | Key, Href string |
| 21 | On bool |
| 22 | } |
| 23 | |
| 24 | func adminTabs(page string) []AdminTab { |
| 25 | tabs := []AdminTab{ |
| 26 | {Key: "admin.tab.users", Href: "/admin/users"}, |
| 27 | {Key: "admin.tab.runner", Href: "/admin/runner"}, |
| 28 | {Key: "admin.tab.storage", Href: "/setup"}, |
| 29 | {Key: "admin.tab.pipelines", Href: "/admin/pipelines"}, |
| 30 | } |
| 31 | for i := range tabs { |
| 32 | tabs[i].On = (page == "adminusers" && i == 0) || (page == "adminrunner" && i == 1) || (page == "setup" && i == 2) || (page == "adminpipelines" && i == 3) |
| 33 | } |
| 34 | return tabs |
| 35 | } |
| 36 | |
| 37 | func isAdminPage(page string) bool { |
| 38 | return page == "adminusers" || page == "adminrunner" || page == "setup" || page == "adminpipelines" |
| 39 | } |
| 40 | |
| 41 | func (s *Server) mountAdmin(mux *http.ServeMux) { |
| 42 | mux.HandleFunc("GET /admin", s.adminHome) |
| 43 | mux.HandleFunc("GET /admin/{$}", s.adminHome) |
| 44 | mux.HandleFunc("GET /admin/users", s.adminUsers) |
| 45 | mux.HandleFunc("POST /admin/users/role", s.adminUserRole) |
| 46 | mux.HandleFunc("GET /admin/pipelines", s.adminPipelines) |
| 47 | mux.HandleFunc("POST /admin/pipelines", s.adminPipelinesSave) |
| 48 | mux.HandleFunc("GET /admin/storage", func(w http.ResponseWriter, r *http.Request) { |
| 49 | if ok, _ := s.adminOnly(w, r); ok { |
| 50 | http.Redirect(w, r, "/setup", http.StatusFound) |
| 51 | } |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | func (s *Server) adminHome(w http.ResponseWriter, r *http.Request) { |
| 56 | if ok, _ := s.adminOnly(w, r); ok { |
| 57 | http.Redirect(w, r, "/admin/users", http.StatusFound) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // AdminUserRow is één rij in Beheer > Gebruikers. |
| 62 | type AdminUserRow struct { |
| 63 | User store.User |
| 64 | Self bool |
| 65 | } |
| 66 | |
| 67 | func (s *Server) adminUsers(w http.ResponseWriter, r *http.Request) { |
| 68 | if ok, _ := s.adminOnly(w, r); !ok { |
| 69 | return |
| 70 | } |
| 71 | users, err := s.Store.ListUsers(r.Context()) |
| 72 | if err != nil { |
| 73 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 74 | return |
| 75 | } |
| 76 | v := s.view(w, r, "title.admin") |
| 77 | v.Page = "adminusers" |
| 78 | me := s.who(r).User.ID |
| 79 | for _, u := range users { |
| 80 | v.AdminUsers = append(v.AdminUsers, AdminUserRow{User: u, Self: u.ID == me}) |
| 81 | } |
| 82 | s.render(w, "adminusers", v) |
| 83 | } |
| 84 | |
| 85 | func adminRoleErr(err error) error { |
| 86 | switch { |
| 87 | case errors.Is(err, store.ErrSelfDemote): |
| 88 | return i18n.E("admin.err.self") |
| 89 | case errors.Is(err, store.ErrLastAdmin): |
| 90 | return i18n.E("admin.err.last") |
| 91 | } |
| 92 | return i18n.E("admin.err.user") |
| 93 | } |
| 94 | |
| 95 | func (s *Server) setAdmin(r *http.Request, target int64, admin bool) (store.User, error) { |
| 96 | by := s.who(r).User |
| 97 | if by.ID == 0 { |
| 98 | by = apiUser(r) |
| 99 | } |
| 100 | u, err := s.Store.SetInstanceAdmin(r.Context(), by.ID, target, admin) |
| 101 | if err != nil { |
| 102 | return u, err |
| 103 | } |
| 104 | if s.Log != nil { |
| 105 | s.Log.Info("admin audit", "action", map[bool]string{true: "grant_admin", false: "revoke_admin"}[admin], "by", by.Username, "target", u.Username) |
| 106 | } |
| 107 | return u, nil |
| 108 | } |
| 109 | |
| 110 | func (s *Server) adminUserRole(w http.ResponseWriter, r *http.Request) { |
| 111 | if ok, _ := s.adminOnly(w, r); !ok || !s.csrfOK(w, r) { |
| 112 | return |
| 113 | } |
| 114 | id, _ := strconv.ParseInt(r.FormValue("id"), 10, 64) |
| 115 | u, err := s.setAdmin(r, id, r.FormValue("admin") == "1") |
| 116 | if err != nil { |
| 117 | s.redirect(w, r, "/admin/users", "", s.trErr(r, adminRoleErr(err))) |
| 118 | return |
| 119 | } |
| 120 | key := "admin.ok.granted" |
| 121 | if !u.InstanceAdmin { |
| 122 | key = "admin.ok.revoked" |
| 123 | } |
| 124 | s.redirect(w, r, "/admin/users", s.tr(r, key, u.Username), "") |
| 125 | } |
| 126 | |
| 127 | func adminUserJSON(u store.User) map[string]any { |
| 128 | return map[string]any{"id": u.ID, "username": u.Username, "admin": u.InstanceAdmin, "created_at": rfcTime(u.CreatedAt)} |
| 129 | } |
| 130 | |
| 131 | func (s *Server) apiAdminUsers(w http.ResponseWriter, r *http.Request) { |
| 132 | if !apiUser(r).InstanceAdmin { |
| 133 | s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden"))) |
| 134 | return |
| 135 | } |
| 136 | users, err := s.Store.ListUsers(r.Context()) |
| 137 | if err != nil { |
| 138 | s.writeAPIErr(w, r, err) |
| 139 | return |
| 140 | } |
| 141 | out := []map[string]any{} |
| 142 | for _, u := range users { |
| 143 | out = append(out, adminUserJSON(u)) |
| 144 | } |
| 145 | writeJSON(w, http.StatusOK, out) |
| 146 | } |
| 147 | |
| 148 | func (s *Server) apiAdminUserRole(w http.ResponseWriter, r *http.Request) { |
| 149 | if !apiUser(r).InstanceAdmin { |
| 150 | s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden"))) |
| 151 | return |
| 152 | } |
| 153 | var in struct { |
| 154 | Admin *bool `json:"admin"` |
| 155 | } |
| 156 | if err := readJSON(r, &in); err != nil || in.Admin == nil { |
| 157 | s.writeAPIErr(w, r, fail(400, i18n.E("admin.err.body"))) |
| 158 | return |
| 159 | } |
| 160 | target, err := s.Store.UserByUsername(r.Context(), strings.TrimSpace(r.PathValue("username"))) |
| 161 | if err != nil { |
| 162 | s.writeAPIErr(w, r, fail(404, i18n.E("admin.err.user"))) |
| 163 | return |
| 164 | } |
| 165 | u, err := s.setAdmin(r, target.ID, *in.Admin) |
| 166 | if err != nil { |
| 167 | code := 409 |
| 168 | if errors.Is(err, store.ErrSelfDemote) { |
| 169 | code = 403 |
| 170 | } |
| 171 | s.writeAPIErr(w, r, fail(code, adminRoleErr(err))) |
| 172 | return |
| 173 | } |
| 174 | writeJSON(w, http.StatusOK, adminUserJSON(u)) |
| 175 | } |