forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permission.go
93 lines (78 loc) · 2.41 KB
/
permission.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
package models
import (
"strconv"
"strings"
"github.com/kamruljpi/go-admin/modules/db"
)
// PermissionModel is permission model structure.
type PermissionModel struct {
Base
Id int64
Name string
Slug string
HttpMethod []string
HttpPath []string
CreatedAt string
UpdatedAt string
}
// Permission return a default permission model.
func Permission() PermissionModel {
return PermissionModel{Base: Base{TableName: "goadmin_permissions"}}
}
// PermissionWithId return a default permission model of given id.
func PermissionWithId(id string) PermissionModel {
idInt, _ := strconv.Atoi(id)
return PermissionModel{Base: Base{TableName: "goadmin_permissions"}, Id: int64(idInt)}
}
func (t PermissionModel) SetConn(con db.Connection) PermissionModel {
t.Conn = con
return t
}
// IsEmpty check the user model is empty or not.
func (t PermissionModel) IsEmpty() bool {
return t.Id == int64(0)
}
// IsSlugExist check the row exist with given slug and id.
func (t PermissionModel) IsSlugExist(slug string, id string) bool {
if id == "" {
check, _ := t.Table(t.TableName).Where("slug", "=", slug).First()
return check != nil
}
check, _ := t.Table(t.TableName).
Where("slug", "=", slug).
Where("id", "!=", id).
First()
return check != nil
}
// Find return the permission model of given id.
func (t PermissionModel) Find(id interface{}) PermissionModel {
item, _ := t.Table(t.TableName).Find(id)
return t.MapToModel(item)
}
// FindBySlug return the permission model of given slug.
func (t PermissionModel) FindBySlug(slug string) PermissionModel {
item, _ := t.Table(t.TableName).Where("slug", "=", slug).First()
return t.MapToModel(item)
}
// FindBySlug return the permission model of given slug.
func (t PermissionModel) FindByName(name string) PermissionModel {
item, _ := t.Table(t.TableName).Where("name", "=", name).First()
return t.MapToModel(item)
}
// MapToModel get the permission model from given map.
func (t PermissionModel) MapToModel(m map[string]interface{}) PermissionModel {
t.Id = m["id"].(int64)
t.Name, _ = m["name"].(string)
t.Slug, _ = m["slug"].(string)
methods, _ := m["http_method"].(string)
if methods != "" {
t.HttpMethod = strings.Split(methods, ",")
} else {
t.HttpMethod = []string{""}
}
path, _ := m["http_path"].(string)
t.HttpPath = strings.Split(path, "\n")
t.CreatedAt, _ = m["created_at"].(string)
t.UpdatedAt, _ = m["updated_at"].(string)
return t
}