This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perm.go
133 lines (114 loc) · 2.48 KB
/
perm.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
package xormperm
import (
"os"
"time"
"github.com/go-xorm/xorm"
)
type Group struct {
Id int64
Name string `xorm:"unique"`
Created time.Time `xorm:"created"`
}
type UserGroup struct {
UserName string `xorm:"pk"`
GroupName string `xorm:"pk"`
}
type Perm struct {
Id int64
Path string `xorm:"unique"`
Owner string
Group string
Mode os.FileMode
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
type XormPerm struct {
engine *xorm.Engine
defaultOwner string
defaultGroup string
defaultMode os.FileMode
}
func New(engine *xorm.Engine, owner, group string, mode os.FileMode) *XormPerm {
return &XormPerm{engine, owner, group, mode}
}
func (db *XormPerm) GetOwner(rPath string) (string, error) {
var perm = Perm{Path: rPath}
has, err := db.engine.Get(&perm)
if err != nil {
return "", err
}
if !has {
return db.defaultOwner, nil
}
return perm.Owner, nil
}
func (db *XormPerm) GetGroup(rPath string) (string, error) {
var perm = Perm{Path: rPath}
has, err := db.engine.Get(&perm)
if err != nil {
return "", err
}
if !has {
return db.defaultGroup, nil
}
return perm.Group, nil
}
func (db *XormPerm) GetMode(rPath string) (os.FileMode, error) {
var perm = Perm{Path: rPath}
has, err := db.engine.Get(&perm)
if err != nil {
return os.ModeType, err
}
if !has {
return db.defaultMode, nil
}
return perm.Mode, nil
}
func (db *XormPerm) ChOwner(rPath, owner string) error {
var perm = Perm{Path: rPath}
has, err := db.engine.Get(&perm)
if err != nil {
return err
}
if !has {
perm.Owner = owner
perm.Group = db.defaultGroup
perm.Mode = db.defaultMode
_, err = db.engine.Insert(&perm)
return err
}
_, err = db.engine.Update(&Perm{Owner: owner}, &perm)
return err
}
func (db *XormPerm) ChGroup(rPath, group string) error {
var perm = Perm{Path: rPath}
has, err := db.engine.Get(&perm)
if err != nil {
return err
}
if !has {
perm.Owner = db.defaultOwner
perm.Group = group
perm.Mode = db.defaultMode
_, err = db.engine.Insert(&perm)
return err
}
_, err = db.engine.Update(&Perm{Group: group}, &perm)
return err
}
func (db *XormPerm) ChMode(rPath string, mode os.FileMode) error {
var perm = Perm{Path: rPath}
has, err := db.engine.Get(&perm)
if err != nil {
return err
}
if !has {
perm.Owner = db.defaultOwner
perm.Group = db.defaultGroup
perm.Mode = mode
_, err = db.engine.Insert(&perm)
return err
}
_, err = db.engine.Update(&Perm{Mode: mode}, &perm)
return err
}