forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
members.go
86 lines (73 loc) · 2.18 KB
/
members.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
package database
import (
. "github.com/drone/drone/pkg/model"
"github.com/russross/meddler"
)
// Name of the Member table in the database
const memberTable = "members"
// SQL Queries to retrieve a list of all members belonging to a team.
const memberStmt = `
SELECT user_id, name, email, gravatar, role
FROM members, users
WHERE users.id = members.user_id
AND team_id = ?
`
// SQL Queries to retrieve a team by id and user.
const memberFindStmt = `
SELECT user_id, name, email, gravatar, role
FROM members, users
WHERE users.id = members.user_id
AND user_id = ?
AND team_id = ?
`
// SQL Queries to retrieve a team by name .
const memberDeleteStmt = `
DELETE FROM members
WHERE user_id = ? AND team_id = ?
`
// SQL Queries to retrieve a member's role by id and user.
const roleFindStmt = `
SELECT id, team_id, user_id, role FROM members
WHERE user_id = ? AND team_id = ?
`
// Returns the Member with the given user and team IDs.
func GetMember(user, team int64) (*Member, error) {
member := Member{}
err := meddler.QueryRow(db, &member, memberFindStmt, user, team)
return &member, err
}
// Returns true if the user is a member of the team
func IsMember(user, team int64) (bool, error) {
role := Role{}
err := meddler.QueryRow(db, &role, roleFindStmt, user, team)
return len(role.Role) > 0, err
}
// Returns true is the user is an admin member of the team.
func IsMemberAdmin(user, team int64) (bool, error) {
role := Role{}
err := meddler.QueryRow(db, &role, roleFindStmt, user, team)
return role.Role == RoleAdmin || role.Role == RoleOwner, err
}
// Creates a new Member.
func SaveMember(user, team int64, role string) error {
r := Role{}
if err := meddler.QueryRow(db, &r, roleFindStmt, user, team); err == nil {
r.Role = role
return meddler.Save(db, memberTable, &r)
}
r.UserID = user
r.TeamID = team
r.Role = role
return meddler.Save(db, memberTable, &r)
}
// Deletes an existing Member.
func DeleteMember(user, team int64) error {
_, err := db.Exec(memberDeleteStmt, user, team)
return err
}
// Returns a list of all Team members.
func ListMembers(team int64) ([]*Member, error) {
var members []*Member
err := meddler.QueryAll(db, &members, memberStmt, team)
return members, err
}