💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › d4dc8235c4c03656ae5d80f2d3… captured on 2023-04-19 at 23:32:56. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

0 package db

1

2 import (

3 "errors"

4 "log"

5 )

6

7 func GetUserGroupAccess(repoID int, userID int) (int, error) {

8 rows, err := db.Query("SELECT a.privilege FROM access a " +

9 "INNER JOIN member m ON a.groupID = m.groupID " +

10 "WHERE a.repoID = ? AND m.userID = ?",

11 repoID, userID)

12 if err != nil {

13 return -1, err

14 }

15 defer rows.Close()

16 privilege := -1

17 for rows.Next() {

18 var p int

19 err = rows.Scan(&p)

20 if err != nil {

21 return -1, err

22 }

23 if p > privilege {

24 privilege = p

25 }

26 }

27 return privilege, nil

28 }

29

30 func GetUserAccess(repoID int, userID int) (int, error) {

31 rows, err := db.Query("SELECT privilege FROM access " +

32 "WHERE repoID = ? AND userID = ? ",

33 repoID, userID)

34 if err != nil {

35 return -1, err

36 }

37 defer rows.Close()

38 if !rows.Next() {

39 return -1, errors.New("The user is not a contributor")

40 }

41 var privilege int

42 err = rows.Scan(&privilege)

43 if err != nil {

44 return -1, err

45 }

46 return privilege, nil

47 }

48

49 func GetAccess(repoID int, userID int) (int, error) {

50 privilege, err := GetUserGroupAccess(repoID, userID)

51 if err != nil {

52 return -1, err

53 }

54 if privilege == 2 {

55 return 2, nil

56 }

57 p, err := GetUserAccess(repoID, userID)

58 if err != nil {

59 return -1, err

60 }

61 if p < privilege {

62 return privilege, nil

63 }

64 return p, nil

65 }

66

67 func SetUserAccess(repoID int, userID int, privilege int) (error) {

68 _, err := db.Exec("UPDATE access SET privilege = ? " +

69 "WHERE repoID = ? AND userID = ?",

70 privilege, repoID, userID)

71 return err

72 }

73

74 func GetRepoAccess(repoID int) ([]Access, error) {

75 rows, err := db.Query("SELECT a.repoID, a.userID, b.name, " +

76 "a.privilege FROM access a " +

77 "INNER JOIN user b ON a.userID = b.userID " +

78 "WHERE a.repoID = ?", repoID)

79 if err != nil {

80 log.Println(err.Error())

81 return nil, err

82 }

83 defer rows.Close()

84 var access []Access

85 for rows.Next() {

86 var a = Access{}

87 err = rows.Scan(&a.RepoID, &a.UserID, &a.Name, &a.Privilege)

88 if err != nil {

89 return nil, err

90 }

91 access = append(access, a)

92 }

93 return access, nil

94 }

95

96 func GetRepoGroupAccess(repoID int) ([]Access, error) {

97 rows, err := db.Query("SELECT a.repoID, a.groupID, b.name, " +

98 "a.privilege FROM access a " +

99 "INNER JOIN groups b ON a.groupID = b.groupID " +

100 "WHERE a.repoID = ?", repoID)

101 if err != nil {

102 log.Println(err.Error())

103 return nil, err

104 }

105 defer rows.Close()

106 var access []Access

107 for rows.Next() {

108 var a = Access{}

109 err = rows.Scan(&a.RepoID, &a.UserID, &a.Name, &a.Privilege)

110 if err != nil {

111 return nil, err

112 }

113 access = append(access, a)

114 }

115 return access, nil

116 }

117

118 func HasReadAccessTo(userID int) ([]Repo, error) {

119 rows, err := db.Query("SELECT r.repoID, r.userID, r.name, " +

120 "r.creation, r.public, r.description, u.Name " +

121 "FROM access a " +

122 "INNER JOIN member m ON a.groupID = m.groupID " +

123 "INNER JOIN repo r ON a.repoID = r.repoID " +

124 "INNER JOIN user u ON r.userID = u.userID " +

125 "WHERE m.userID = ? AND a.privilege > 0 " +

126 "AND ? <> r.userID " +

127 "UNION " +

128 "SELECT r.repoID, r.userID, r.name, " +

129 "r.creation, r.public, r.description, u.Name " +

130 "FROM access a " +

131 "INNER JOIN repo r ON a.repoID = r.repoID " +

132 "INNER JOIN user u ON r.userID = u.userID " +

133 "WHERE a.userID = ? AND a.privilege > 0 " +

134 "AND ? <> r.userID",

135 userID, userID, userID, userID)

136 if err != nil {

137 return nil, err

138 }

139 defer rows.Close()

140 repos := []Repo{}

141 for rows.Next() {

142 var r Repo

143 err = rows.Scan(&r.RepoID, &r.UserID, &r.Name, &r.Date,

144 &r.IsPublic, &r.Description, &r.Username)

145 if err != nil {

146 return nil, err

147 }

148 repos = append(repos, r)

149 }

150 return repos, nil

151 }

152

153 func GetGroupAccess(repoID int, groupID int) (int, error) {

154 rows, err := db.Query("SELECT privilege FROM access " +

155 "WHERE repoID = ? AND groupID = ? ",

156 repoID, groupID)

157 if err != nil {

158 return -1, err

159 }

160 defer rows.Close()

161 if !rows.Next() {

162 return -1, errors.New("The group is not a contributor")

163 }

164 var privilege int

165 err = rows.Scan(&privilege)

166 if err != nil {

167 return -1, err

168 }

169 return privilege, nil

170 }

171

172 func SetGroupAccess(repoID int, groupID int, privilege int) (error) {

173 _, err := db.Exec("UPDATE access SET privilege = ? " +

174 "WHERE repoID = ? AND groupID = ?",

175 privilege, repoID, groupID)

176 return err

177 }

178

179 func RemoveGroupAccess(repoID int, groupID int) error {

180 statement, err := db.Exec("DELETE FROM access " +

181 "WHERE groupID = ? AND repoID = ?",

182 groupID, repoID)

183 if err != nil {

184 return err

185 }

186 rows, err := statement.RowsAffected()

187 if err != nil {

188 return err

189 }

190 if rows < 1 {

191 return errors.New("The group is not a contributor")

192 }

193 return nil

194

195 }

196

197 func AddUserAccess(owner int, repoID int, user string) error {

198 userID, err := GetUserID(user)

199 if err != nil {

200 return err

201 }

202 if userID == owner {

203 return errors.New(

204 "The repository owner already has maximum privilege")

205 }

206

207 rows, err := db.Query("SELECT privilege FROM access " +

208 "WHERE repoID = ? AND userID = ? ",

209 repoID, userID)

210 if err != nil {

211 return err

212 }

213 defer rows.Close()

214 if rows.Next() {

215 return errors.New("The user already has access")

216 }

217

218 _, err = db.Exec("INSERT INTO access (repoID, userID, privilege) " +

219 "VALUES(?, ?, 1)", repoID, userID)

220 if err != nil {

221 log.Println(err.Error())

222 }

223 return err

224 }

225

226 func AddGroupAccess(repoID int, group string) error {

227 groupID, err := GetGroupID(group)

228 if err != nil {

229 return err

230 }

231

232 rows, err := db.Query("SELECT privilege FROM access " +

233 "WHERE repoID = ? AND groupID = ? ",

234 repoID, groupID)

235 if err != nil {

236 return err

237 }

238 defer rows.Close()

239 if rows.Next() {

240 return errors.New("The group already has access")

241 }

242

243 _, err = db.Exec("INSERT INTO access (repoID, groupID, privilege) " +

244 "VALUES(?, ?, 1)", repoID, groupID)

245 if err != nil {

246 log.Println(err.Error())

247 }

248 return err

249 }

250

251 func RemoveUserAccess(repoID int, userID int) error {

252 statement, err := db.Exec("DELETE FROM access " +

253 "WHERE userID = ? AND repoID = ?",

254 userID, repoID)

255 if err != nil {

256 return err

257 }

258 rows, err := statement.RowsAffected()

259 if err != nil {

260 return err

261 }

262 if rows < 1 {

263 return errors.New("The user is not a contributor")

264 }

265 return nil

266 }

267