1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-13 22:00:27 +00:00
mycorrhiza/user/users.go
handlerug b87583ef28
Drop fixed authorization
Important changes:
- UseFixedAuth is now UseAuth and toggles all kinds of authorization and
  registration
- UseRegistration is now AllowRegistration to better reflect the meaning
- LimitRegistration is now RegistrationLimit because it's not a boolean,
  it's a value (not "limit registration?", but "registration limit is
  ...")
- registered-users.json is now users.json, because all users are stored
  there
- user.AuthUsed is dropped in favor of new cfg.UseAuth which has the
  same meaning

I hope I have not forgotten anything.
2021-07-02 15:20:03 +07:00

72 lines
1.3 KiB
Go

package user
import "sync"
var users sync.Map
var tokens sync.Map
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 {
filtered := []string{}
for u := range YieldUsers() {
if u.Group == group {
filtered = append(filtered, u.Name)
}
}
return filtered
}
func Count() (i uint64) {
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)
}
return EmptyUser()
}
func UserByName(username string) *User {
if userUntyped, ok := users.Load(username); ok {
user := userUntyped.(*User)
return user
}
return EmptyUser()
}
func commenceSession(username, token string) {
tokens.Store(token, username)
dumpTokens()
}
func terminateSession(token string) {
tokens.Delete(token)
dumpTokens()
}