1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-03-10 13:38:20 +00:00
Timur Ismagilov 41733c50bd
New templates #117 (#236)
Didn't have the chance to migrate //all// templates just yet. We'll get there.

* Implement yet another template system

* Move orphans to the new system and fix a bug in it

* Link orphans in the admin panel

* Move the backlink handlers to the web package

* Move auth routing to web

* Move /user-list to the new system

* Move change password and translate it

* Move stuff

* Move admin-related stuff to the web

* Move a lot of files into internal dir

Outside of it are web and stuff that needs further refactoring

* Fix static not loading and de-qtpl tree

* Move tree to internal

* Keep the globe on the same line #230

* Revert "Keep the globe on the same line #230"

This reverts commit ae78e5e459b1e980ba89bf29e61f75c0625ed2c7.

* Migrate templates from hypview: delete, edit, start empty and existing WIP

The delete media view was removed, I didn't even know it still existed as a GET. A rudiment.

* Make views multi-file and break compilation

* Megarefactoring of hypha views

* Auth-related stuffs

* Fix some of those weird imports

* Migrate cat views

* Fix cat js

* Lower standards

* Internalize trauma
2024-09-07 21:22:41 +03:00

130 lines
2.5 KiB
Go

package user
import (
"encoding/json"
"errors"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"log"
"os"
"github.com/bouncepaw/mycorrhiza/internal/files"
"github.com/bouncepaw/mycorrhiza/util"
)
// InitUserDatabase loads users, if necessary. Call it during initialization.
func InitUserDatabase() {
ReadUsersFromFilesystem()
}
// ReadUsersFromFilesystem reads all user information from filesystem and
// stores it internally.
func ReadUsersFromFilesystem() {
if cfg.UseAuth {
rememberUsers(usersFromFile())
readTokensToUsers()
}
}
func usersFromFile() []*User {
var users []*User
contents, err := os.ReadFile(files.UserCredentialsJSON())
if errors.Is(err, os.ErrNotExist) {
return users
}
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(contents, &users)
if err != nil {
log.Fatal(err)
}
for _, u := range users {
u.Name = util.CanonicalName(u.Name)
if u.Source == "" {
u.Source = "local"
}
}
log.Println("Found", len(users), "users")
return users
}
func rememberUsers(userList []*User) {
for _, user := range userList {
if !IsValidUsername(user.Name) {
continue
}
users.Store(user.Name, user)
}
}
func readTokensToUsers() {
contents, err := os.ReadFile(files.TokensJSON())
if os.IsNotExist(err) {
return
}
if err != nil {
log.Fatal(err)
}
var tmp map[string]string
err = json.Unmarshal(contents, &tmp)
if err != nil {
log.Fatal(err)
}
for token, username := range tmp {
tokens.Store(token, username)
// commenceSession(username, token)
}
log.Println("Found", len(tmp), "active sessions")
}
// SaveUserDatabase stores current user credentials into JSON file by configured path.
func SaveUserDatabase() error {
return dumpUserCredentials()
}
func dumpUserCredentials() error {
var userList []*User
// TODO: lock the map during saving to prevent corruption
for u := range YieldUsers() {
userList = append(userList, u)
}
blob, err := json.MarshalIndent(userList, "", "\t")
if err != nil {
log.Println(err)
return err
}
err = os.WriteFile(files.UserCredentialsJSON(), blob, 0666)
if err != nil {
log.Println(err)
return err
}
return nil
}
func dumpTokens() {
tmp := make(map[string]string)
tokens.Range(func(k, v interface{}) bool {
token := k.(string)
username := v.(string)
tmp[token] = username
return true
})
blob, err := json.MarshalIndent(tmp, "", "\t")
if err != nil {
log.Println(err)
return
}
err = os.WriteFile(files.TokensJSON(), blob, 0666)
if err != nil {
log.Println("an error occurred in dumpTokens function:", err)
}
}