mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-09 16:25:15 +03:00
add a basic web interface
The builtin web interface allows to manage users and connections
This commit is contained in:
@@ -51,11 +51,12 @@ const (
|
||||
var (
|
||||
// SupportedProviders data provider configured in the sftpgo.conf file must match of these strings
|
||||
SupportedProviders = []string{SQLiteDataProviderName, PGSQLDataProviderName, MySQLDataProviderName, BoltDataProviderName}
|
||||
config Config
|
||||
provider Provider
|
||||
sqlPlaceholders []string
|
||||
validPerms = []string{PermAny, PermListItems, PermDownload, PermUpload, PermDelete, PermRename,
|
||||
PermCreateDirs, PermCreateSymlinks, PermOverwrite}
|
||||
// ValidPerms list that contains all the valid permissions for an user
|
||||
ValidPerms = []string{PermAny, PermListItems, PermDownload, PermUpload, PermOverwrite, PermRename, PermDelete,
|
||||
PermCreateDirs, PermCreateSymlinks}
|
||||
config Config
|
||||
provider Provider
|
||||
sqlPlaceholders []string
|
||||
hashPwdPrefixes = []string{argonPwdPrefix, bcryptPwdPrefix, pbkdf2SHA1Prefix, pbkdf2SHA256Prefix,
|
||||
pbkdf2SHA512Prefix, sha512cryptPwdPrefix}
|
||||
pbkdfPwdPrefixes = []string{pbkdf2SHA1Prefix, pbkdf2SHA256Prefix, pbkdf2SHA512Prefix}
|
||||
@@ -276,13 +277,25 @@ func buildUserHomeDir(user *User) {
|
||||
}
|
||||
}
|
||||
|
||||
func validatePermissions(user *User) error {
|
||||
for _, p := range user.Permissions {
|
||||
if !utils.IsStringInSlice(p, ValidPerms) {
|
||||
return &ValidationError{err: fmt.Sprintf("Invalid permission: %v", p)}
|
||||
}
|
||||
}
|
||||
if utils.IsStringInSlice(PermAny, user.Permissions) {
|
||||
user.Permissions = []string{PermAny}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateUser(user *User) error {
|
||||
buildUserHomeDir(user)
|
||||
if len(user.Username) == 0 || len(user.HomeDir) == 0 {
|
||||
return &ValidationError{err: "Mandatory parameters missing"}
|
||||
}
|
||||
if len(user.Password) == 0 && len(user.PublicKeys) == 0 {
|
||||
return &ValidationError{err: "Please set password or at least a public_key"}
|
||||
return &ValidationError{err: "Please set a password or at least a public_key"}
|
||||
}
|
||||
if len(user.Permissions) == 0 {
|
||||
return &ValidationError{err: "Please grant some permissions to this user"}
|
||||
@@ -290,10 +303,8 @@ func validateUser(user *User) error {
|
||||
if !filepath.IsAbs(user.HomeDir) {
|
||||
return &ValidationError{err: fmt.Sprintf("home_dir must be an absolute path, actual value: %v", user.HomeDir)}
|
||||
}
|
||||
for _, p := range user.Permissions {
|
||||
if !utils.IsStringInSlice(p, validPerms) {
|
||||
return &ValidationError{err: fmt.Sprintf("Invalid permission: %v", p)}
|
||||
}
|
||||
if err := validatePermissions(user); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(user.Password) > 0 && !utils.IsStringPrefixInSlice(user.Password, hashPwdPrefixes) {
|
||||
pwd, err := argon2id.CreateHash(user.Password, argon2id.DefaultParams)
|
||||
|
||||
@@ -2,7 +2,9 @@ package dataprovider
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/drakkan/sftpgo/utils"
|
||||
)
|
||||
@@ -123,3 +125,67 @@ func (u *User) GetRelativePath(path string) string {
|
||||
}
|
||||
return "/" + filepath.ToSlash(rel)
|
||||
}
|
||||
|
||||
// GetQuotaSummary returns used quota and limits if defined
|
||||
func (u *User) GetQuotaSummary() string {
|
||||
var result string
|
||||
result = "Files: " + strconv.Itoa(u.UsedQuotaFiles)
|
||||
if u.QuotaFiles > 0 {
|
||||
result += "/" + strconv.Itoa(u.QuotaFiles)
|
||||
}
|
||||
if u.UsedQuotaSize > 0 || u.QuotaSize > 0 {
|
||||
result += ". Size: " + utils.ByteCountSI(u.UsedQuotaSize)
|
||||
if u.QuotaSize > 0 {
|
||||
result += "/" + utils.ByteCountSI(u.QuotaSize)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetPermissionsAsString returns the user's permissions as comma separated string
|
||||
func (u *User) GetPermissionsAsString() string {
|
||||
var result string
|
||||
for _, p := range u.Permissions {
|
||||
if len(result) > 0 {
|
||||
result += ", "
|
||||
}
|
||||
result += p
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetBandwidthAsString returns bandwidth limits if defines
|
||||
func (u *User) GetBandwidthAsString() string {
|
||||
result := "Download: "
|
||||
if u.DownloadBandwidth > 0 {
|
||||
result += utils.ByteCountSI(u.DownloadBandwidth*1000) + "/s."
|
||||
} else {
|
||||
result += "ulimited."
|
||||
}
|
||||
result += " Upload: "
|
||||
if u.UploadBandwidth > 0 {
|
||||
result += utils.ByteCountSI(u.UploadBandwidth*1000) + "/s."
|
||||
} else {
|
||||
result += "ulimited."
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetInfoString returns user's info as string.
|
||||
// Number of public keys, max sessions, uid and gid are returned
|
||||
func (u *User) GetInfoString() string {
|
||||
var result string
|
||||
if len(u.PublicKeys) > 0 {
|
||||
result += fmt.Sprintf("Public keys: %v ", len(u.PublicKeys))
|
||||
}
|
||||
if u.MaxSessions > 0 {
|
||||
result += fmt.Sprintf("Max sessions: %v ", u.MaxSessions)
|
||||
}
|
||||
if u.UID > 0 {
|
||||
result += fmt.Sprintf("UID: %v ", u.UID)
|
||||
}
|
||||
if u.GID > 0 {
|
||||
result += fmt.Sprintf("GID: %v ", u.GID)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user