gowiki/pkg/db/helper.go

64 lines
1 KiB
Go

// Copyright (C) 2019 Marius Schellenberger
package db
import (
"crypto/sha256"
"git.giftfish.de/ston1th/gowiki/pkg/core"
"time"
)
var reservedNames = []string{
"blacklist",
"login",
"logout",
"new",
"search",
"sections",
"user",
"view",
"wiki",
}
// Parts taken from strings.TrimPrefix
func hasTrimPrefix(s, prefix string) (string, bool) {
if len(s) >= len(prefix) && s[0:len(prefix)] == prefix {
return s[len(prefix):], true
}
return s, false
}
func addOrRemove(s string, a []string) []string {
i, _ := core.Contains(s, a)
switch i {
case -1:
return append(a, s)
case 0:
return a[1:]
case len(a):
return a[:i]
default:
return append(a[:i], a[i+1:]...)
}
}
func validatePassword(pw string) (b []byte) {
b = []byte(pw)
if len(pw) <= 56 {
return
}
hash := sha256.New()
hash.Write(b)
b = hash.Sum(nil)
return
}
const timeFmt = "2006-01-02 15:04:05"
func now() string {
return time.Now().Format(timeFmt)
}
func created(username string) string {
return username + " " + now()
}