USER
package model
import (
"fmt"
"time"
"github.com/insionng/zenpress/helper"
"github.com/casbin/casbin"
)
// User 用户基本信息表。存放系统所有用户基本信息。
type User struct {
ID uint64 `gorm:"primary_key"`
Token string //`orm:"unique"`
UserLogin string `gorm:"not null default '' index VARCHAR(60)"`
UserPass string `gorm:"not null default '' VARCHAR(255)"`
UserNicename string `gorm:"not null default '' index VARCHAR(50)"`
UserEmail string `gorm:"not null default '' index VARCHAR(100)"`
UserURL string `gorm:"not null default '' VARCHAR(100)"`
UserRegistered time.Time `gorm:"not null default '0000-00-00 00:00:00' DATETIME"`
UserActivationKey string `gorm:"not null default '' VARCHAR(255)"`
UserStatus int `gorm:"not null default 0 INT(11)"`
DisplayName string `gorm:"not null default '' VARCHAR(250)"`
Spam int `gorm:"not null default 0 TINYINT(2)"`
Deleted int `gorm:"not null default 0 TINYINT(2)"`
Roles []Role `gorm:"many2many:user_roles;"`
}
var Enforcer *casbin.Enforcer = nil
func getAttr(name string, attr string) string {
if attr != "url" {
return ""
}
permissions := FindPermissions()
for _, permission := range permissions {
if name == fmt.Sprintf("%v", permission.ID) {
return permission.URL
}
}
return ""
}
func getAttrFunc(args ...interface{}) (interface{}, error) {
name := args[0].(string)
attr := args[1].(string)
return (string)(getAttr(name, attr)), nil
}
func Init() {
Enforcer = &casbin.Enforcer{}
Enforcer.InitWithFile("content/config/rbac_model.conf", "")
Enforcer.AddActionAttributeFunction(getAttrFunc)
type UserRoleResult struct {
UserID uint64
RoleID uint64
}
var res []UserRoleResult
Database.Raw("select user_id, role_id from user_roles").Scan(&res)
for _, param := range res {
Enforcer.AddRoleForUser(fmt.Sprintf("%v", param.UserID), fmt.Sprintf("%v", param.RoleID))
}
type RolePermissionResult struct {
RoleID uint64
PermissionID uint64
}
var rez []RolePermissionResult
Database.Raw("select role_id, permission_id from role_permissions").Scan(&rez)
for _, param := range rez {
Enforcer.AddPermissionForUser(fmt.Sprintf("%v", param.RoleID), fmt.Sprintf("%v", param.PermissionID))
}
}
func FindUserById(id int) (bool, User) {
var user User
db := Database.Where("id = ?", id).First(&user)
return db.Error == nil, user
}
func FindUserByToken(token string) (bool, User) {
var user User
db := Database.Where("token = ?", token).First(&user)
return db.Error == nil, user
}
func Login(username string, password string) (bool, User) {
var user User
db := Database.Where("username = ? and password = ?", username, password).First(&user)
return db.Error == nil, user
}
func FindUserByUserName(username string) (bool, User) {
var user User
db := Database.Where("username = ?", username).First(&user)
return db.Error == nil, user
}
func SaveUser(user *User) uint64 {
Database.Create(user)
return user.ID
}
func UpdateUser(user *User) {
Database.Update(user)
}
func PageUser(p int, size int) helper.Page {
var user User
var list []User
qs := Database.Find(&user)
var count int
qs.Count(count)
qs.Related(&user).Order("user_registered").Limit(size).Offset((p - 1) * size).Find(&list)
//c, _ := strconv.Atoi(strconv.FormatInt(count, 10))
return helper.PageUtil(count, p, size, list)
}
func FindPermissionByUserIdAndPermissionName(userId int, name string) bool {
permissions := FindPermissions()
for _, permission := range permissions {
if name == permission.Name {
return Enforcer.Enforce(fmt.Sprintf("%v", userId), permission.URL)
}
}
return false
}
func DeleteUser(user *User) {
Enforcer.DeleteUser(fmt.Sprintf("%v", user.ID))
Database.Delete(user)
}
func DeleteUserRolesByUserId(user_id int) {
Enforcer.DeleteRolesForUser(fmt.Sprintf("%v", user_id))
Database.Exec("delete from user_roles where user_id = ?", user_id)
}
func SaveUserRole(user_id int, role_id int) {
Enforcer.AddRoleForUser(fmt.Sprintf("%v", user_id), fmt.Sprintf("%v", role_id))
Database.Exec("insert into user_roles (user_id, role_id) values (?, ?)", user_id, role_id)
}
type UserRoleResult struct {
ID uint64
UserID uint64
RoleID uint64
}
func FindUserRolesByUserId(user_id int) []UserRoleResult {
var res []UserRoleResult
Database.Raw("select id, user_id, role_id from user_roles where user_id = ?", user_id).Scan(&res)
return res
}
permission
package model
import (
"fmt"
)
type Permission struct {
ID uint64 `gorm:"primary_key"`
Pid uint64
URL string
Name string
Description string
Roles []*Role
ChildPermissions []*Permission `gorm:"-"`
}
func FindPermissionById(id int) Permission {
var permission Permission
Database.Where("id = ?", id).First(&permission)
return permission
}
func FindPermissions() []*Permission {
var permissions []*Permission
Database.Find(&permissions)
return permissions
}
func FindPermissionsByPid(pid int) []*Permission {
var permissions []*Permission
Database.Where("pid = ?", pid).Find(&permissions)
return permissions
}
func SavePermission(permission *Permission) uint64 {
Database.Create(permission)
return permission.ID
}
func UpdatePermission(permission *Permission) uint64 {
Database.Update(permission)
return permission.ID
}
func DeletePermission(permission *Permission) {
Enforcer.DeletePermission(fmt.Sprintf("%v", permission.ID))
Database.Delete(permission)
}
func DeleteRolePermissionByPermissionId(permission_id int) {
Enforcer.DeletePermission(fmt.Sprintf("%v", permission_id))
Database.Exec("delete from role_permissions where permission_id = ?", permission_id)
}
roles
package model
import (
"fmt"
"strconv"
)
type Role struct {
ID uint64 `gorm:"primary_key"`
Name string // `orm:"unique"`
Users []*User
Permissions []*Permission `gorm:"many2many:role_permissions;"`
}
func FindRoleById(id int) Role {
var role Role
Database.Where("id = ?", id).First(&role)
return role
}
func FindRoles() []*Role {
var roles []*Role
Database.Find(&roles)
return roles
}
func SaveRole(role *Role) uint64 {
Database.Create(role)
return role.ID
}
func UpdateRole(role *Role) {
Database.Update(role, "name")
}
func DeleteRole(role *Role) {
Enforcer.DeleteRole(fmt.Sprintf("%v", role.ID))
Database.Delete(role)
}
func DeleteRolePermissionByRoleId(role_id int) {
Enforcer.DeletePermissionsForUser(strconv.Itoa(role_id))
Database.Exec("delete from role_permissions where role_id = ?", role_id)
}
func SaveRolePermission(role_id int, permission_id int) {
Enforcer.AddPermissionForUser(fmt.Sprintf("%v", role_id), fmt.Sprintf("%v", permission_id))
Database.Exec("insert into role_permissions (role_id, permission_id) values (?, ?)", role_id, permission_id)
}
type Result struct {
ID uint64
RoleID uint64
PermissionID uint64
}
func FindRolePermissionByRoleId(role_id int) []Result {
var res []Result
Database.Raw("select id, role_id, permission_id from role_permissions where role_id = ?", role_id).Scan(&res)
return res
}
网友评论