package auth import ( "git.aiterp.net/rpdata/api/internal/store" "github.com/globalsign/mgo" ) var userCollection *mgo.Collection // A User represents user information about a user that has logged in. type User struct { ID string `bson:"_id" json:"id"` Nick string `bson:"nick,omitempty" json:"nick,omitempty"` Permissions []string `bson:"permissions" json:"permissions"` } // Permitted returns true if either of the permissions can be found // // `token.UserID == page.Author || token.Permitted("story.edit")` func (user *User) Permitted(permissions ...string) bool { for i := range permissions { for j := range user.Permissions { if permissions[i] == user.Permissions[j] { return true } } } return false } // FindUser finds a user by userid func FindUser(userid string) (User, error) { user := User{} err := userCollection.FindId(userid).One(&user) if err == mgo.ErrNotFound { user := User{ ID: userid, Nick: "", Permissions: []string{ "member", "log.edit", "post.edit", "post.move", "file.upload", }, } err := userCollection.Insert(user) if err != nil { return User{}, err } } return user, err } func init() { store.HandleInit(func(db *mgo.Database) { userCollection = db.C("core.users") }) }