opengist/internal/web/admin.go

237 lines
6.8 KiB
Go
Raw Permalink Normal View History

2023-03-14 15:22:52 +00:00
package web
import (
"github.com/labstack/echo/v4"
"github.com/thomiceli/opengist/internal/actions"
2023-05-15 19:07:29 +00:00
"github.com/thomiceli/opengist/internal/config"
2023-09-02 22:30:57 +00:00
"github.com/thomiceli/opengist/internal/db"
2023-05-15 19:07:29 +00:00
"github.com/thomiceli/opengist/internal/git"
2023-03-14 15:22:52 +00:00
"runtime"
2023-03-17 13:56:39 +00:00
"strconv"
"time"
2023-03-14 15:22:52 +00:00
)
func adminIndex(ctx echo.Context) error {
2024-05-04 22:24:25 +00:00
setData(ctx, "htmlTitle", trH(ctx, "admin.admin_panel"))
2023-03-14 15:22:52 +00:00
setData(ctx, "adminHeaderPage", "index")
setData(ctx, "opengistVersion", config.OpengistVersion)
setData(ctx, "goVersion", runtime.Version())
gitVersion, err := git.GetGitVersion()
if err != nil {
return errorRes(500, "Cannot get git version", err)
}
setData(ctx, "gitVersion", gitVersion)
2023-09-02 22:30:57 +00:00
countUsers, err := db.CountAll(&db.User{})
2023-03-14 15:22:52 +00:00
if err != nil {
return errorRes(500, "Cannot count users", err)
}
setData(ctx, "countUsers", countUsers)
2023-09-02 22:30:57 +00:00
countGists, err := db.CountAll(&db.Gist{})
2023-03-14 15:22:52 +00:00
if err != nil {
return errorRes(500, "Cannot count gists", err)
}
setData(ctx, "countGists", countGists)
2023-09-02 22:30:57 +00:00
countKeys, err := db.CountAll(&db.SSHKey{})
2023-03-14 15:22:52 +00:00
if err != nil {
return errorRes(500, "Cannot count SSH keys", err)
}
setData(ctx, "countKeys", countKeys)
setData(ctx, "syncReposFromFS", actions.IsRunning(actions.SyncReposFromFS))
setData(ctx, "syncReposFromDB", actions.IsRunning(actions.SyncReposFromDB))
setData(ctx, "gitGcRepos", actions.IsRunning(actions.GitGcRepos))
setData(ctx, "syncGistPreviews", actions.IsRunning(actions.SyncGistPreviews))
setData(ctx, "resetHooks", actions.IsRunning(actions.ResetHooks))
2024-01-04 02:38:15 +00:00
setData(ctx, "indexGists", actions.IsRunning(actions.IndexGists))
2023-03-14 15:22:52 +00:00
return html(ctx, "admin_index.html")
}
func adminUsers(ctx echo.Context) error {
2024-05-04 22:24:25 +00:00
setData(ctx, "htmlTitle", trH(ctx, "admin.users")+" - "+trH(ctx, "admin.admin_panel"))
2023-03-14 15:22:52 +00:00
setData(ctx, "adminHeaderPage", "users")
pageInt := getPage(ctx)
2023-09-02 22:30:57 +00:00
var data []*db.User
2023-03-14 15:22:52 +00:00
var err error
2023-09-02 22:30:57 +00:00
if data, err = db.GetAllUsers(pageInt - 1); err != nil {
2023-03-14 15:22:52 +00:00
return errorRes(500, "Cannot get users", err)
}
2023-04-16 22:35:04 +00:00
if err = paginate(ctx, data, pageInt, 10, "data", "admin-panel/users", 1); err != nil {
2024-05-04 22:24:25 +00:00
return errorRes(404, tr(ctx, "error.page-not-found"), nil)
2023-03-14 15:22:52 +00:00
}
return html(ctx, "admin_users.html")
}
func adminGists(ctx echo.Context) error {
2024-05-04 22:24:25 +00:00
setData(ctx, "htmlTitle", trH(ctx, "admin.gists")+" - "+trH(ctx, "admin.admin_panel"))
2023-03-14 15:22:52 +00:00
setData(ctx, "adminHeaderPage", "gists")
pageInt := getPage(ctx)
2023-09-02 22:30:57 +00:00
var data []*db.Gist
2023-03-14 15:22:52 +00:00
var err error
2023-09-02 22:30:57 +00:00
if data, err = db.GetAllGists(pageInt - 1); err != nil {
2023-03-14 15:22:52 +00:00
return errorRes(500, "Cannot get gists", err)
}
2023-04-16 22:35:04 +00:00
if err = paginate(ctx, data, pageInt, 10, "data", "admin-panel/gists", 1); err != nil {
2024-05-04 22:24:25 +00:00
return errorRes(404, tr(ctx, "error.page-not-found"), nil)
2023-03-14 15:22:52 +00:00
}
return html(ctx, "admin_gists.html")
}
func adminUserDelete(ctx echo.Context) error {
2023-03-17 13:56:39 +00:00
userId, _ := strconv.ParseUint(ctx.Param("user"), 10, 64)
2023-09-02 22:30:57 +00:00
user, err := db.GetUserById(uint(userId))
2023-03-17 13:56:39 +00:00
if err != nil {
return errorRes(500, "Cannot retrieve user", err)
}
if err := user.Delete(); err != nil {
2023-03-14 15:22:52 +00:00
return errorRes(500, "Cannot delete this user", err)
}
2024-05-04 22:24:25 +00:00
addFlash(ctx, tr(ctx, "flash.admin.user-deleted"), "success")
return redirect(ctx, "/admin-panel/users")
2023-03-14 15:22:52 +00:00
}
func adminGistDelete(ctx echo.Context) error {
2023-09-02 22:30:57 +00:00
gist, err := db.GetGistByID(ctx.Param("gist"))
2023-03-14 15:22:52 +00:00
if err != nil {
return errorRes(500, "Cannot retrieve gist", err)
}
if err = gist.DeleteRepository(); err != nil {
2023-03-14 15:22:52 +00:00
return errorRes(500, "Cannot delete the repository", err)
}
2023-03-17 13:56:39 +00:00
if err = gist.Delete(); err != nil {
2023-03-14 15:22:52 +00:00
return errorRes(500, "Cannot delete this gist", err)
}
2024-01-04 02:38:15 +00:00
gist.RemoveFromIndex()
2024-05-04 22:24:25 +00:00
addFlash(ctx, tr(ctx, "flash.admin.gist-deleted"), "success")
return redirect(ctx, "/admin-panel/gists")
}
func adminSyncReposFromFS(ctx echo.Context) error {
2024-05-04 22:24:25 +00:00
addFlash(ctx, tr(ctx, "flash.admin.sync-fs"), "success")
go actions.Run(actions.SyncReposFromFS)
return redirect(ctx, "/admin-panel")
}
2023-03-14 15:22:52 +00:00
func adminSyncReposFromDB(ctx echo.Context) error {
2024-05-04 22:24:25 +00:00
addFlash(ctx, tr(ctx, "flash.admin.sync-db"), "success")
go actions.Run(actions.SyncReposFromDB)
return redirect(ctx, "/admin-panel")
2023-03-14 15:22:52 +00:00
}
2023-09-04 09:11:54 +00:00
func adminGcRepos(ctx echo.Context) error {
2024-05-04 22:24:25 +00:00
addFlash(ctx, tr(ctx, "flash.admin.git-gc"), "success")
go actions.Run(actions.GitGcRepos)
return redirect(ctx, "/admin-panel")
}
func adminSyncGistPreviews(ctx echo.Context) error {
2024-05-04 22:24:25 +00:00
addFlash(ctx, tr(ctx, "flash.admin.sync-previews"), "success")
go actions.Run(actions.SyncGistPreviews)
return redirect(ctx, "/admin-panel")
}
func adminResetHooks(ctx echo.Context) error {
2024-05-04 22:24:25 +00:00
addFlash(ctx, tr(ctx, "flash.admin.reset-hooks"), "success")
go actions.Run(actions.ResetHooks)
2023-09-04 09:11:54 +00:00
return redirect(ctx, "/admin-panel")
}
2024-01-04 02:38:15 +00:00
func adminIndexGists(ctx echo.Context) error {
2024-05-04 22:24:25 +00:00
addFlash(ctx, tr(ctx, "flash.admin.index-gists"), "success")
2024-01-04 02:38:15 +00:00
go actions.Run(actions.IndexGists)
return redirect(ctx, "/admin-panel")
}
2023-06-07 18:50:30 +00:00
func adminConfig(ctx echo.Context) error {
2024-05-04 22:24:25 +00:00
setData(ctx, "htmlTitle", trH(ctx, "admin.configuration")+" - "+trH(ctx, "admin.admin_panel"))
2023-06-07 18:50:30 +00:00
setData(ctx, "adminHeaderPage", "config")
2023-05-11 13:16:05 +00:00
setData(ctx, "dbtype", db.DatabaseInfo.Type.String())
setData(ctx, "dbname", db.DatabaseInfo.Database)
2023-06-07 18:50:30 +00:00
return html(ctx, "admin_config.html")
2023-05-11 13:16:05 +00:00
}
2023-06-07 18:50:30 +00:00
func adminSetConfig(ctx echo.Context) error {
key := ctx.FormValue("key")
value := ctx.FormValue("value")
2023-09-02 22:30:57 +00:00
if err := db.UpdateSetting(key, value); err != nil {
return errorRes(500, "Cannot set setting", err)
}
return ctx.JSON(200, map[string]interface{}{
"success": true,
})
}
func adminInvitations(ctx echo.Context) error {
2024-05-04 22:24:25 +00:00
setData(ctx, "htmlTitle", trH(ctx, "admin.invitations")+" - "+trH(ctx, "admin.admin_panel"))
setData(ctx, "adminHeaderPage", "invitations")
var invitations []*db.Invitation
var err error
if invitations, err = db.GetAllInvitations(); err != nil {
return errorRes(500, "Cannot get invites", err)
}
setData(ctx, "invitations", invitations)
return html(ctx, "admin_invitations.html")
}
func adminInvitationsCreate(ctx echo.Context) error {
code := ctx.FormValue("code")
nbMax, err := strconv.ParseUint(ctx.FormValue("nbMax"), 10, 64)
if err != nil {
nbMax = 10
}
expiresAtUnix, err := strconv.ParseInt(ctx.FormValue("expiredAtUnix"), 10, 64)
if err != nil {
expiresAtUnix = time.Now().Unix() + 604800 // 1 week
}
invitation := &db.Invitation{
Code: code,
ExpiresAt: expiresAtUnix,
NbMax: uint(nbMax),
}
if err := invitation.Create(); err != nil {
return errorRes(500, "Cannot create invitation", err)
}
2024-05-04 22:24:25 +00:00
addFlash(ctx, tr(ctx, "flash.admin.invitation-created"), "success")
return redirect(ctx, "/admin-panel/invitations")
}
func adminInvitationsDelete(ctx echo.Context) error {
id, _ := strconv.ParseUint(ctx.Param("id"), 10, 64)
invitation, err := db.GetInvitationByID(uint(id))
if err != nil {
return errorRes(500, "Cannot retrieve invitation", err)
}
if err := invitation.Delete(); err != nil {
return errorRes(500, "Cannot delete this invitation", err)
}
2024-05-04 22:24:25 +00:00
addFlash(ctx, tr(ctx, "flash.admin.invitation-deleted"), "success")
return redirect(ctx, "/admin-panel/invitations")
}