58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package authorser
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"91porn-server/models/v/adminmod"
|
|
"91porn-server/models/v/authoritymod"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
// ObjectID ObjectID
|
|
type ObjectID = primitive.ObjectID
|
|
|
|
// SetAuthority SetAuthority
|
|
func SetAuthority(adminID ObjectID, authID ObjectID) error {
|
|
existed, err := adminmod.ExistsByID(adminID)
|
|
if err != nil {
|
|
return fmt.Errorf("service SetAuthority adminID Exists error: " + err.Error())
|
|
}
|
|
if !existed {
|
|
return fmt.Errorf("service SetAuthority adminID is not exist")
|
|
}
|
|
existed, err = authoritymod.ExistsByID(authID)
|
|
if err != nil {
|
|
return fmt.Errorf("service SetAuthority authID Exists error: " + err.Error())
|
|
}
|
|
if !existed {
|
|
return fmt.Errorf("service SetAuthority authID is not exist")
|
|
}
|
|
authority, err := authoritymod.FindOne(authoritymod.AuthorityDoc{
|
|
ID: authID,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("service SetAuthority authID is not existed" + err.Error())
|
|
}
|
|
if err = adminmod.UpdateByID(adminID, adminmod.AdminDoc{
|
|
Role: &authority.Role,
|
|
}); err != nil {
|
|
return fmt.Errorf("service SetAuthority faild " + err.Error())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func UnsetAuthority(adminID ObjectID) error {
|
|
existed, err := adminmod.ExistsByID(adminID)
|
|
if err != nil {
|
|
return fmt.Errorf("author UnsetAuthority adminID Exists error: " + err.Error())
|
|
}
|
|
if !existed {
|
|
return fmt.Errorf("service UnsetAuthority adminID is not exist")
|
|
}
|
|
if err = adminmod.UnsetPrivilege(adminID); err != nil {
|
|
return fmt.Errorf("service UnsetAuthority faild " + err.Error())
|
|
}
|
|
return nil
|
|
}
|