-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
169 lines (135 loc) · 3.33 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
166
167
168
169
/*
* Bulldozer Framework
* Copyright (C) DesertBit
*/
package auth
import (
"fmt"
)
//###################//
//### User Struct ###//
//###################//
type Users []*User
type User struct {
u *dbUser
}
func newUser(u *dbUser) *User {
return &User{
u: u,
}
}
func (u *User) ID() string {
return u.u.ID
}
func (u *User) LoginName() string {
return u.u.LoginName
}
func (u *User) Name() string {
return u.u.Name
}
func (u *User) EMail() string {
return u.u.EMail
}
func (u *User) Enabled() bool {
return u.u.Enabled
}
func (u *User) LastLogin() int64 {
return u.u.LastLogin
}
func (u *User) Created() int64 {
return u.u.Created
}
// IsSysOp returns a boolean if the user is a system operator.
func (u *User) IsSysOp() bool {
return u.IsInGroup(GroupSysOp)
}
// IsAdmin returns a boolean if the user is an administrator.
func (u *User) IsAdmin() bool {
return u.IsInGroup(GroupAdmin)
}
func (u *User) Groups() []string {
return u.u.Groups
}
// IsInGroup returns true if the user is in one of the passed groups.
// True is returned if no groups are passed.
func (u *User) IsInGroup(groups ...string) bool {
if len(groups) == 0 {
return true
}
for _, group := range groups {
for _, userGroup := range u.u.Groups {
if group == userGroup {
return true
}
}
}
return false
}
// IsInGroups accepts a slice instead of variadic arguments.
// This might be useful if called from templates directly.
func (u *User) IsInGroups(groups []string) bool {
return u.IsInGroup(groups...)
}
// Update the user data, by retreiving the data from the database.
func (u *User) Update() error {
// Obtain the user value from the database with the user ID.
dbUser, err := dbGetUserByID(u.u.ID)
if err != nil {
return err
} else if dbUser == nil {
return fmt.Errorf("failed to update user data: user does not exists with ID: '%s'", u.u.ID)
}
// Set the new value.
u.u = dbUser
return nil
}
//#################################//
//### User manipulation methods ###//
//#################################//
// AddGroup adds the user to the group.
// You have to call the commit method to make this persistent.
func (u *User) AddGroup(groups ...string) {
// TODO: Validate if the groups exists?
// Only add the groups, if they don't exist already.
var found bool
for _, group := range groups {
found = false
for _, userGroup := range u.u.Groups {
if group == userGroup {
found = true
break
}
}
if !found {
u.u.Groups = append(u.u.Groups, group)
}
}
}
// SetName sets the user's name.
// You have to call the commit method to make this persistent.
func (u *User) SetName(name string) {
u.u.Name = name
}
// SetLoginName sets the user's login name.
// You have to call the commit method to make this persistent.
func (u *User) SetLoginName(loginName string) {
u.u.LoginName = loginName
}
// SetEMail sets the user's e-mail.
// You have to call the commit method to make this persistent.
func (u *User) SetEMail(email string) {
u.u.EMail = email
}
// SetEnabled activates or disables the user.
// You have to call the commit method to make this persistent.
func (u *User) SetEnabled(enabled bool) {
u.u.Enabled = enabled
}
// Commit all changes to the database.
func (u *User) Commit() error {
err := dbUpdateUser(u.u)
if err != nil {
return fmt.Errorf("auth: failed to commit user changes: %v", err)
}
return nil
}