1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 05:20:26 +00:00
mycorrhiza/user/users.go

76 lines
1.3 KiB
Go
Raw Normal View History

package user
import (
"sync"
)
var AuthUsed bool
var users sync.Map
var tokens sync.Map
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
}
func ListUsersWithGroup(group string) []string {
usersWithTheGroup := []string{}
2021-01-30 18:21:50 +00:00
for u := range YieldUsers() {
if u.Group == group {
usersWithTheGroup = append(usersWithTheGroup, u.Name)
}
2021-01-30 18:21:50 +00:00
}
return usersWithTheGroup
}
func Count() int {
i := 0
users.Range(func(k, v interface{}) bool {
i++
return true
})
return i
}
func HasUsername(username string) bool {
_, has := users.Load(username)
return has
}
func CredentialsOK(username, password string) bool {
return userByName(username).isCorrectPassword(password)
}
func userByToken(token string) *User {
if usernameUntyped, ok := tokens.Load(token); ok {
username := usernameUntyped.(string)
return userByName(username)
}
2021-01-24 07:30:14 +00:00
return EmptyUser()
}
func userByName(username string) *User {
if userUntyped, ok := users.Load(username); ok {
user := userUntyped.(*User)
return user
}
2021-01-24 07:30:14 +00:00
return EmptyUser()
}
func commenceSession(username, token string) {
tokens.Store(token, username)
go dumpTokens()
}
func terminateSession(token string) {
tokens.Delete(token)
go dumpTokens()
}