-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathuser.go
165 lines (133 loc) · 4.46 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
}