2022-03-12 18:06:39 +00:00
|
|
|
// package models
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// import (
|
|
|
|
// "errors"
|
|
|
|
// "fmt"
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// "github.com/globalsign/mgo/bson"
|
|
|
|
// )
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //User model
|
|
|
|
// type User struct {
|
|
|
|
// ID bson.ObjectId `bson:"_id,omitempty"`
|
|
|
|
// Email string `bson:"email"`
|
|
|
|
// Name string `bson:"name"`
|
|
|
|
// Username string `bson:"username"`
|
|
|
|
// Password string `bson:"password"`
|
|
|
|
// PrimaryGroup bson.ObjectId `bson:"primarygroup,omitempty"`
|
|
|
|
// Groups []bson.ObjectId `bson:"groups,omitempty"`
|
|
|
|
// Session string
|
|
|
|
// }
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //Create user
|
|
|
|
// func (u *User) Create() error {
|
|
|
|
// var user User
|
|
|
|
// read("user", "email", u.Email, &user)
|
|
|
|
// if u.Email == user.Email {
|
|
|
|
// return errors.New("Email all ready used")
|
|
|
|
// }
|
|
|
|
// return create("user", &u)
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// }
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //Read user
|
|
|
|
// func (u *User) Read(key string, value interface{}) error {
|
|
|
|
// err := read("user", key, value, &u)
|
|
|
|
// if err != nil {
|
|
|
|
// return errors.New("User doesn't exist")
|
|
|
|
// }
|
|
|
|
// return nil
|
|
|
|
// }
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //ReadAll users
|
|
|
|
// func (u *User) ReadAll() ([]User, error) {
|
|
|
|
// var users []User
|
|
|
|
// var err error
|
|
|
|
// err = readAll("user", "", nil, &users)
|
|
|
|
// return users, err
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// }
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //Update user
|
|
|
|
// func (u *User) Update() error {
|
|
|
|
// return update("user", "_id", u.ID, &u)
|
|
|
|
// }
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //Delete user
|
|
|
|
// func (u *User) Delete(key string, value string) error {
|
|
|
|
// err := delete("user", key, value)
|
|
|
|
// return err
|
|
|
|
// }
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //HasPermission check if a given user is admin or has a given permisssion
|
|
|
|
// func (u *User) HasPermission(perm string) bool {
|
|
|
|
// var group Group
|
|
|
|
// //Check primary group
|
|
|
|
// err := group.Read("_id", u.PrimaryGroup)
|
|
|
|
// fmt.Println(group.Admin)
|
|
|
|
// if err == nil && (group.Admin == true || group.Permissions[perm] == true) {
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// return true
|
|
|
|
// }
|
|
|
|
// //Check other groups
|
|
|
|
// for id := range u.Groups {
|
|
|
|
// err = group.Read("_id", id)
|
|
|
|
// if err == nil && (group.Admin || group.Permissions[perm]) {
|
|
|
|
// return true
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return false
|
|
|
|
// }
|