2020-04-29 17:44:29 -07:00
|
|
|
package manage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gochan-org/gochan/pkg/gclog"
|
|
|
|
"github.com/gochan-org/gochan/pkg/gcsql"
|
|
|
|
"github.com/gochan-org/gochan/pkg/serverutil"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-05-28 12:49:41 -07:00
|
|
|
sSuccess = iota
|
2020-04-29 17:44:29 -07:00
|
|
|
sInvalidPassword
|
|
|
|
sOtherError
|
|
|
|
)
|
|
|
|
|
2020-07-09 15:54:31 -07:00
|
|
|
func createSession(key, username, password string, request *http.Request, writer http.ResponseWriter) int {
|
2020-04-29 17:44:29 -07:00
|
|
|
//returns 0 for successful, 1 for password mismatch, and 2 for other
|
|
|
|
domain := request.Host
|
2020-06-06 09:28:45 -07:00
|
|
|
var err error
|
2020-04-29 17:44:29 -07:00
|
|
|
domain = chopPortNumRegex.Split(domain, -1)[0]
|
|
|
|
|
|
|
|
if !serverutil.ValidReferer(request) {
|
|
|
|
gclog.Print(gclog.LStaffLog, "Rejected login from possible spambot @ "+request.RemoteAddr)
|
2020-05-28 12:49:41 -07:00
|
|
|
return sOtherError
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
staff, err := gcsql.GetStaffByName(username)
|
|
|
|
if err != nil {
|
|
|
|
gclog.Print(gclog.LErrorLog, err.Error())
|
2020-05-28 12:49:41 -07:00
|
|
|
return sInvalidPassword
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
success := bcrypt.CompareHashAndPassword([]byte(staff.PasswordChecksum), []byte(password))
|
|
|
|
if success == bcrypt.ErrMismatchedHashAndPassword {
|
|
|
|
// password mismatch
|
|
|
|
gclog.Print(gclog.LStaffLog, "Failed login (password mismatch) from "+request.RemoteAddr+" at "+time.Now().Format(gcsql.MySQLDatetimeFormat))
|
2020-05-28 12:49:41 -07:00
|
|
|
return sInvalidPassword
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// successful login, add cookie that expires in one month
|
|
|
|
http.SetCookie(writer, &http.Cookie{
|
|
|
|
Name: "sessiondata",
|
|
|
|
Value: key,
|
|
|
|
Path: "/",
|
|
|
|
Domain: domain,
|
|
|
|
MaxAge: 60 * 60 * 24 * 7,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err = gcsql.CreateSession(key, username); err != nil {
|
|
|
|
gclog.Print(gclog.LErrorLog, "Error creating new staff session: ", err.Error())
|
2020-05-28 12:49:41 -07:00
|
|
|
return sOtherError
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
|
2020-05-28 12:49:41 -07:00
|
|
|
return sSuccess
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
|
2020-06-06 09:28:45 -07:00
|
|
|
func getCurrentStaff(request *http.Request) (string, error) { //TODO after refactor, check if still used
|
2020-04-29 17:44:29 -07:00
|
|
|
sessionCookie, err := request.Cookie("sessiondata")
|
|
|
|
if err != nil {
|
2020-06-06 09:28:45 -07:00
|
|
|
return "", err
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
name, err := gcsql.GetStaffName(sessionCookie.Value)
|
|
|
|
if err == nil {
|
2020-06-06 09:28:45 -07:00
|
|
|
return "", err
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2020-06-06 09:28:45 -07:00
|
|
|
func getCurrentFullStaff(request *http.Request) (*gcsql.Staff, error) {
|
2020-04-29 17:44:29 -07:00
|
|
|
sessionCookie, err := request.Cookie("sessiondata")
|
|
|
|
if err != nil {
|
2020-06-06 09:28:45 -07:00
|
|
|
return nil, err
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
return gcsql.GetStaffBySession(sessionCookie.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStaffRank returns the rank number of the staff referenced in the request
|
|
|
|
func GetStaffRank(request *http.Request) int {
|
|
|
|
staff, err := getCurrentFullStaff(request)
|
|
|
|
if err != nil {
|
2020-10-10 16:17:36 -07:00
|
|
|
return NoPerms
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
return staff.Rank
|
|
|
|
}
|
2020-05-22 18:06:15 +02:00
|
|
|
|
2020-10-10 16:17:36 -07:00
|
|
|
func getStaffMenu(writer http.ResponseWriter, request *http.Request) (string, error) {
|
|
|
|
var links string
|
|
|
|
rank := GetStaffRank(request)
|
|
|
|
for f, mf := range actions {
|
|
|
|
if rank < mf.Permissions || mf.Permissions == NoPerms {
|
|
|
|
continue
|
2020-05-22 18:06:15 +02:00
|
|
|
}
|
2020-10-10 16:17:36 -07:00
|
|
|
links += `<a href="manage?action=` + f + `" id="` + f + `">` + mf.Title + `</a></br />`
|
2020-05-22 18:06:15 +02:00
|
|
|
}
|
2020-10-10 16:17:36 -07:00
|
|
|
return links, nil
|
2020-05-22 18:06:15 +02:00
|
|
|
}
|