2021-01-09 20:49:48 +00:00
|
|
|
package user
|
|
|
|
|
2021-07-02 08:20:03 +00:00
|
|
|
import "sync"
|
2021-01-09 20:49:48 +00:00
|
|
|
|
|
|
|
var users sync.Map
|
|
|
|
var tokens sync.Map
|
|
|
|
|
2021-10-01 17:12:16 +00:00
|
|
|
// YieldUsers creates a channel which iterates existing users.
|
2021-01-30 18:21:50 +00:00
|
|
|
func YieldUsers() chan *User {
|
|
|
|
ch := make(chan *User)
|
|
|
|
go func(ch chan *User) {
|
|
|
|
users.Range(func(_, v interface{}) bool {
|
|
|
|
ch <- v.(*User)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
close(ch)
|
|
|
|
}(ch)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:12:16 +00:00
|
|
|
// ListUsersWithGroup returns a slice with users of desired group.
|
2021-01-23 13:45:17 +00:00
|
|
|
func ListUsersWithGroup(group string) []string {
|
2021-10-29 09:00:19 +00:00
|
|
|
var filtered []string
|
2021-01-30 18:21:50 +00:00
|
|
|
for u := range YieldUsers() {
|
|
|
|
if u.Group == group {
|
2021-07-02 08:20:03 +00:00
|
|
|
filtered = append(filtered, u.Name)
|
2021-01-23 13:45:17 +00:00
|
|
|
}
|
2021-01-30 18:21:50 +00:00
|
|
|
}
|
2021-07-02 08:20:03 +00:00
|
|
|
return filtered
|
2021-01-23 13:45:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 17:12:16 +00:00
|
|
|
// Count returns total users count
|
2021-07-02 08:20:03 +00:00
|
|
|
func Count() (i uint64) {
|
2021-01-23 13:45:17 +00:00
|
|
|
users.Range(func(k, v interface{}) bool {
|
|
|
|
i++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:12:16 +00:00
|
|
|
// HasUsername checks whether the desired user exists
|
2021-01-09 20:49:48 +00:00
|
|
|
func HasUsername(username string) bool {
|
|
|
|
_, has := users.Load(username)
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:12:16 +00:00
|
|
|
// CredentialsOK checks whether a correct user-password pair is provided
|
2021-01-09 20:49:48 +00:00
|
|
|
func CredentialsOK(username, password string) bool {
|
2021-10-01 17:12:16 +00:00
|
|
|
return ByName(username).isCorrectPassword(password)
|
2021-01-09 20:49:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 17:12:16 +00:00
|
|
|
// ByToken finds a user by provided session token
|
|
|
|
func ByToken(token string) *User {
|
|
|
|
// TODO: Needs more session data -- chekoopa
|
2021-01-09 20:49:48 +00:00
|
|
|
if usernameUntyped, ok := tokens.Load(token); ok {
|
|
|
|
username := usernameUntyped.(string)
|
2021-10-01 17:12:16 +00:00
|
|
|
return ByName(username)
|
2021-01-09 20:49:48 +00:00
|
|
|
}
|
2021-01-24 07:30:14 +00:00
|
|
|
return EmptyUser()
|
2021-01-09 20:49:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 17:12:16 +00:00
|
|
|
// ByName finds a user by one's username
|
|
|
|
func ByName(username string) *User {
|
2021-01-09 20:49:48 +00:00
|
|
|
if userUntyped, ok := users.Load(username); ok {
|
|
|
|
user := userUntyped.(*User)
|
|
|
|
return user
|
|
|
|
}
|
2021-01-24 07:30:14 +00:00
|
|
|
return EmptyUser()
|
2021-01-09 20:49:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 17:12:16 +00:00
|
|
|
// DeleteUser removes a user by one's name and saves user database.
|
2021-07-02 14:04:00 +00:00
|
|
|
func DeleteUser(name string) error {
|
|
|
|
user, loaded := users.LoadAndDelete(name)
|
|
|
|
if loaded {
|
|
|
|
u := user.(*User)
|
|
|
|
u.Name = "anon"
|
|
|
|
u.Group = "anon"
|
|
|
|
u.Password = ""
|
|
|
|
return SaveUserDatabase()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-09 20:49:48 +00:00
|
|
|
func commenceSession(username, token string) {
|
|
|
|
tokens.Store(token, username)
|
2021-03-07 14:58:37 +00:00
|
|
|
dumpTokens()
|
2021-01-09 20:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func terminateSession(token string) {
|
|
|
|
tokens.Delete(token)
|
2021-03-07 14:58:37 +00:00
|
|
|
dumpTokens()
|
2021-01-09 20:49:48 +00:00
|
|
|
}
|