github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/members.go (about) 1 package database 2 3 import ( 4 . "github.com/drone/drone/pkg/model" 5 "github.com/russross/meddler" 6 ) 7 8 // Name of the Member table in the database 9 const memberTable = "members" 10 11 // SQL Queries to retrieve a list of all members belonging to a team. 12 const memberStmt = ` 13 SELECT user_id, name, email, gravatar, role 14 FROM members, users 15 WHERE users.id = members.user_id 16 AND team_id = ? 17 ` 18 19 // SQL Queries to retrieve a team by id and user. 20 const memberFindStmt = ` 21 SELECT user_id, name, email, gravatar, role 22 FROM members, users 23 WHERE users.id = members.user_id 24 AND user_id = ? 25 AND team_id = ? 26 ` 27 28 // SQL Queries to retrieve a team by name . 29 const memberDeleteStmt = ` 30 DELETE FROM members 31 WHERE user_id = ? AND team_id = ? 32 ` 33 34 // SQL Queries to retrieve a member's role by id and user. 35 const roleFindStmt = ` 36 SELECT id, team_id, user_id, role FROM members 37 WHERE user_id = ? AND team_id = ? 38 ` 39 40 // Returns the Member with the given user and team IDs. 41 func GetMember(user, team int64) (*Member, error) { 42 member := Member{} 43 err := meddler.QueryRow(db, &member, memberFindStmt, user, team) 44 return &member, err 45 } 46 47 // Returns true if the user is a member of the team 48 func IsMember(user, team int64) (bool, error) { 49 role := Role{} 50 err := meddler.QueryRow(db, &role, roleFindStmt, user, team) 51 return len(role.Role) > 0, err 52 } 53 54 // Returns true is the user is an admin member of the team. 55 func IsMemberAdmin(user, team int64) (bool, error) { 56 role := Role{} 57 err := meddler.QueryRow(db, &role, roleFindStmt, user, team) 58 return role.Role == RoleAdmin || role.Role == RoleOwner, err 59 } 60 61 // Creates a new Member. 62 func SaveMember(user, team int64, role string) error { 63 r := Role{} 64 if err := meddler.QueryRow(db, &r, roleFindStmt, user, team); err == nil { 65 r.Role = role 66 return meddler.Save(db, memberTable, &r) 67 } 68 69 r.UserID = user 70 r.TeamID = team 71 r.Role = role 72 return meddler.Save(db, memberTable, &r) 73 } 74 75 // Deletes an existing Member. 76 func DeleteMember(user, team int64) error { 77 _, err := db.Exec(memberDeleteStmt, user, team) 78 return err 79 } 80 81 // Returns a list of all Team members. 82 func ListMembers(team int64) ([]*Member, error) { 83 var members []*Member 84 err := meddler.QueryAll(db, &members, memberStmt, team) 85 return members, err 86 }