| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "sort" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/access" |
| 12 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 13 | "gitlab.com/Timfa/bluegit/internal/names" |
| 14 | ) |
| 15 | |
| 16 | // Group is een verzameling repositories en leden. |
| 17 | type Group struct { |
| 18 | ID int64 |
| 19 | Name string |
| 20 | Path string |
| 21 | Description string |
| 22 | Visibility string |
| 23 | DefaultRole access.Role |
| 24 | LogoMIME string |
| 25 | CreatedBy int64 |
| 26 | OwnerID int64 |
| 27 | CreatedAt time.Time |
| 28 | } |
| 29 | |
| 30 | // HasLogo meldt of er een geüpload logo is. |
| 31 | func (g Group) HasLogo() bool { return g.LogoMIME != "" } |
| 32 | |
| 33 | // LogoURL is het pad van het groepslogo, of leeg. |
| 34 | func (g Group) LogoURL() string { |
| 35 | if !g.HasLogo() { |
| 36 | return "" |
| 37 | } |
| 38 | return "/groups/" + g.Path + "/logo" |
| 39 | } |
| 40 | |
| 41 | // Repo is een git-repository van een gebruiker of een groep. |
| 42 | type Repo struct { |
| 43 | ID int64 |
| 44 | OwnerKind string |
| 45 | OwnerID int64 |
| 46 | OwnerPath string |
| 47 | Name string |
| 48 | Description string |
| 49 | DefaultBranch string |
| 50 | Visibility string |
| 51 | LogoMIME string |
| 52 | CreatedAt time.Time |
| 53 | // ReleaseVisibility is "inherit" (zelfde als de repo), "private" of |
| 54 | // "public". Zie ReleasesPublic. |
| 55 | ReleaseVisibility string |
| 56 | // PushedAt is het moment van de laatste push. Nul: nog nooit gepusht. |
| 57 | PushedAt time.Time |
| 58 | } |
| 59 | |
| 60 | // ReleasesPublic meldt of releases (pagina, notities, assets) voor |
| 61 | // niet-leden zichtbaar zijn. Broncode valt hier nooit onder. |
| 62 | func (r Repo) ReleasesPublic() bool { |
| 63 | switch r.ReleaseVisibility { |
| 64 | case "public": |
| 65 | return true |
| 66 | case "private": |
| 67 | return false |
| 68 | default: |
| 69 | return r.Visibility == "public" |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // ValidReleaseVisibility kent inherit, private en public. |
| 74 | func ValidReleaseVisibility(v string) bool { |
| 75 | return v == "inherit" || v == "private" || v == "public" |
| 76 | } |
| 77 | |
| 78 | // SetReleaseVisibility zet de zichtbaarheid van releases. |
| 79 | func (s *Store) SetReleaseVisibility(ctx context.Context, id int64, v string) error { |
| 80 | if !ValidReleaseVisibility(v) { |
| 81 | return i18n.E("err.releaseVisibility") |
| 82 | } |
| 83 | _, err := s.db.ExecContext(ctx, `UPDATE repos SET release_visibility = ? WHERE id = ?`, v, id) |
| 84 | return err |
| 85 | } |
| 86 | |
| 87 | // ListReleaseOnlyRepoIDs geeft privé repositories met publieke releases |
| 88 | // waar deze gebruiker geen lid van is (0 = anoniem). Alleen naam, korte |
| 89 | // beschrijving en de Releases-pagina zijn voor hen zichtbaar. |
| 90 | func (s *Store) ListReleaseOnlyRepoIDs(ctx context.Context, userID int64) ([]int64, error) { |
| 91 | rows, err := s.db.QueryContext(ctx, `SELECT id FROM repos WHERE visibility = 'private' AND release_visibility = 'public' ORDER BY id DESC`) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | var ids []int64 |
| 96 | for rows.Next() { |
| 97 | var id int64 |
| 98 | if err := rows.Scan(&id); err != nil { |
| 99 | rows.Close() |
| 100 | return nil, err |
| 101 | } |
| 102 | ids = append(ids, id) |
| 103 | } |
| 104 | rows.Close() |
| 105 | if err := rows.Err(); err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | var out []int64 |
| 109 | for _, id := range ids { |
| 110 | role, err := s.EffectiveRole(ctx, userID, id) |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | if !role.CanRead() { |
| 115 | out = append(out, id) |
| 116 | } |
| 117 | } |
| 118 | return out, nil |
| 119 | } |
| 120 | |
| 121 | // HasLogo meldt of er een geüpload logo is. |
| 122 | func (r Repo) HasLogo() bool { return r.LogoMIME != "" } |
| 123 | |
| 124 | // LogoURL is het pad van het repologo, of leeg. |
| 125 | func (r Repo) LogoURL() string { |
| 126 | if !r.HasLogo() { |
| 127 | return "" |
| 128 | } |
| 129 | return "/" + r.FullPath() + "/-/logo" |
| 130 | } |
| 131 | |
| 132 | // FullPath is owner/naam. |
| 133 | func (r Repo) FullPath() string { return r.OwnerPath + "/" + r.Name } |
| 134 | |
| 135 | // CreateGroup maakt een groep. De maker wordt beheerder. |
| 136 | // defaultRole is de rol die leden krijgen op een repository die daarna ontstaat. |
| 137 | func (s *Store) CreateGroup(ctx context.Context, creator int64, name, path, description string, defaultRole access.Role) (Group, error) { |
| 138 | name = strings.TrimSpace(name) |
| 139 | path = strings.ToLower(strings.TrimSpace(path)) |
| 140 | if name == "" { |
| 141 | return Group{}, i18n.E("err.groupNeedsName") |
| 142 | } |
| 143 | if defaultRole == "" { |
| 144 | defaultRole = access.RoleDeveloper |
| 145 | } |
| 146 | if _, err := access.ParseRole(string(defaultRole)); err != nil { |
| 147 | return Group{}, err |
| 148 | } |
| 149 | if err := names.ValidGroupPath(path); err != nil { |
| 150 | return Group{}, err |
| 151 | } |
| 152 | if err := s.ensurePathFree(ctx, path); err != nil { |
| 153 | return Group{}, err |
| 154 | } |
| 155 | res, err := s.db.ExecContext(ctx, `INSERT INTO groups (name, path, description, visibility, default_role, created_by, owner_id, created_at) VALUES (?, ?, ?, 'private', ?, ?, ?, ?)`, |
| 156 | name, path, strings.TrimSpace(description), string(defaultRole), creator, creator, now()) |
| 157 | if err != nil { |
| 158 | return Group{}, err |
| 159 | } |
| 160 | id, _ := res.LastInsertId() |
| 161 | if _, err := s.db.ExecContext(ctx, `INSERT INTO group_members (group_id, user_id, role) VALUES (?, ?, ?)`, id, creator, string(access.RoleMaintainer)); err != nil { |
| 162 | return Group{}, err |
| 163 | } |
| 164 | return s.GroupByID(ctx, id) |
| 165 | } |
| 166 | |
| 167 | func (s *Store) ensurePathFree(ctx context.Context, path string) error { |
| 168 | var n int |
| 169 | if err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM users WHERE username = ?`, path).Scan(&n); err != nil { |
| 170 | return err |
| 171 | } |
| 172 | if n > 0 { |
| 173 | return i18n.E("err.pathUser", path) |
| 174 | } |
| 175 | if err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM groups WHERE path = ?`, path).Scan(&n); err != nil { |
| 176 | return err |
| 177 | } |
| 178 | if n > 0 { |
| 179 | return i18n.E("err.pathGroup", path) |
| 180 | } |
| 181 | return nil |
| 182 | } |
| 183 | |
| 184 | // GroupByPath leest een groep. |
| 185 | func (s *Store) GroupByPath(ctx context.Context, path string) (Group, error) { |
| 186 | row := s.db.QueryRowContext(ctx, `SELECT `+groupCols+` FROM groups WHERE path = ?`, path) |
| 187 | return scanGroup(row) |
| 188 | } |
| 189 | |
| 190 | // GroupByID leest een groep. |
| 191 | func (s *Store) GroupByID(ctx context.Context, id int64) (Group, error) { |
| 192 | row := s.db.QueryRowContext(ctx, `SELECT `+groupCols+` FROM groups WHERE id = ?`, id) |
| 193 | return scanGroup(row) |
| 194 | } |
| 195 | |
| 196 | const groupCols = `id, name, path, description, created_by, created_at, visibility, default_role, logo_mime, owner_id` |
| 197 | |
| 198 | func scanGroup(row scanner) (Group, error) { |
| 199 | var g Group |
| 200 | var created, role string |
| 201 | if err := row.Scan(&g.ID, &g.Name, &g.Path, &g.Description, &g.CreatedBy, &created, &g.Visibility, &role, &g.LogoMIME, &g.OwnerID); err != nil { |
| 202 | if errors.Is(err, sql.ErrNoRows) { |
| 203 | return Group{}, errNotFound |
| 204 | } |
| 205 | return Group{}, err |
| 206 | } |
| 207 | g.DefaultRole = access.Role(role) |
| 208 | g.CreatedAt = parseTime(created) |
| 209 | if g.OwnerID == 0 { |
| 210 | g.OwnerID = g.CreatedBy |
| 211 | } |
| 212 | return g, nil |
| 213 | } |
| 214 | |
| 215 | // GroupRole is de rol van een gebruiker in een groep. |
| 216 | // Installatiebeheer geeft geen lidmaatschap. |
| 217 | func (s *Store) GroupRole(ctx context.Context, groupID, userID int64) (access.Role, error) { |
| 218 | if userID == 0 { |
| 219 | return access.RoleNone, nil |
| 220 | } |
| 221 | g, err := s.GroupByID(ctx, groupID) |
| 222 | if err != nil { |
| 223 | return access.RoleNone, err |
| 224 | } |
| 225 | if userID == g.OwnerID { |
| 226 | return access.RoleMaintainer, nil |
| 227 | } |
| 228 | var role string |
| 229 | err = s.db.QueryRowContext(ctx, `SELECT role FROM group_members WHERE group_id = ? AND user_id = ?`, groupID, userID).Scan(&role) |
| 230 | if errors.Is(err, sql.ErrNoRows) { |
| 231 | return access.RoleNone, nil |
| 232 | } |
| 233 | if err != nil { |
| 234 | return access.RoleNone, err |
| 235 | } |
| 236 | return access.Role(role), nil |
| 237 | } |
| 238 | |
| 239 | // ListGroups geeft de groepen waarvan deze gebruiker lid is. |
| 240 | func (s *Store) ListGroups(ctx context.Context, userID int64) ([]Group, error) { |
| 241 | q := `SELECT ` + groupCols + ` FROM groups WHERE id IN (SELECT group_id FROM group_members WHERE user_id = ?) ORDER BY path` |
| 242 | args := []any{userID} |
| 243 | rows, err := s.db.QueryContext(ctx, q, args...) |
| 244 | if err != nil { |
| 245 | return nil, err |
| 246 | } |
| 247 | defer rows.Close() |
| 248 | var out []Group |
| 249 | for rows.Next() { |
| 250 | g, err := scanGroup(rows) |
| 251 | if err != nil { |
| 252 | return nil, err |
| 253 | } |
| 254 | out = append(out, g) |
| 255 | } |
| 256 | return out, rows.Err() |
| 257 | } |
| 258 | |
| 259 | // CreateRepo maakt de metadata. De schijfmap maakt de aanroeper. |
| 260 | func (s *Store) CreateRepo(ctx context.Context, ownerKind string, ownerID int64, name, description, defaultBranch, visibility string) (Repo, error) { |
| 261 | if ownerKind != "user" && ownerKind != "group" { |
| 262 | return Repo{}, i18n.E("err.ownerKind") |
| 263 | } |
| 264 | if err := names.ValidRepoName(name); err != nil { |
| 265 | return Repo{}, err |
| 266 | } |
| 267 | if visibility != "public" && visibility != "private" { |
| 268 | return Repo{}, i18n.E("err.visibility") |
| 269 | } |
| 270 | if defaultBranch == "" { |
| 271 | defaultBranch = "master" |
| 272 | } |
| 273 | if ownerKind == "user" { |
| 274 | u, err := s.UserByID(ctx, ownerID) |
| 275 | if err != nil { |
| 276 | return Repo{}, err |
| 277 | } |
| 278 | var n int |
| 279 | if err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM groups WHERE path = ?`, u.Username).Scan(&n); err != nil { |
| 280 | return Repo{}, err |
| 281 | } |
| 282 | if n > 0 { |
| 283 | return Repo{}, i18n.E("err.userGroupClash", u.Username) |
| 284 | } |
| 285 | } |
| 286 | res, err := s.db.ExecContext(ctx, `INSERT INTO repos (owner_kind, owner_id, name, description, default_branch, visibility, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)`, |
| 287 | ownerKind, ownerID, name, strings.TrimSpace(description), defaultBranch, visibility, now()) |
| 288 | if err != nil { |
| 289 | if strings.Contains(err.Error(), "UNIQUE") { |
| 290 | return Repo{}, i18n.E("err.repoExists", name) |
| 291 | } |
| 292 | return Repo{}, err |
| 293 | } |
| 294 | id, _ := res.LastInsertId() |
| 295 | if err := s.AddProtected(ctx, id, defaultBranch); err != nil { |
| 296 | return Repo{}, err |
| 297 | } |
| 298 | if ownerKind == "group" { |
| 299 | if err := s.GrantDefaultRoles(ctx, ownerID, id); err != nil { |
| 300 | return Repo{}, err |
| 301 | } |
| 302 | } |
| 303 | return s.RepoByID(ctx, id) |
| 304 | } |
| 305 | |
| 306 | // RepoByID leest een repository. |
| 307 | func (s *Store) RepoByID(ctx context.Context, id int64) (Repo, error) { |
| 308 | row := s.db.QueryRowContext(ctx, repoSelect+` WHERE r.id = ?`, id) |
| 309 | return scanRepo(row) |
| 310 | } |
| 311 | |
| 312 | // RepoByPath leest owner/naam. |
| 313 | func (s *Store) RepoByPath(ctx context.Context, owner, name string) (Repo, error) { |
| 314 | row := s.db.QueryRowContext(ctx, repoSelect+` |
| 315 | WHERE r.name = ? AND ( |
| 316 | (r.owner_kind = 'user' AND r.owner_id = (SELECT id FROM users WHERE username = ?)) |
| 317 | OR (r.owner_kind = 'group' AND r.owner_id = (SELECT id FROM groups WHERE path = ?)) |
| 318 | )`, name, owner, owner) |
| 319 | return scanRepo(row) |
| 320 | } |
| 321 | |
| 322 | const repoSelect = ` |
| 323 | SELECT r.id, r.owner_kind, r.owner_id, |
| 324 | CASE r.owner_kind WHEN 'user' THEN (SELECT username FROM users WHERE id = r.owner_id) ELSE (SELECT path FROM groups WHERE id = r.owner_id) END, |
| 325 | r.name, r.description, r.default_branch, r.visibility, r.created_at, r.logo_mime, r.release_visibility, r.pushed_at |
| 326 | FROM repos r` |
| 327 | |
| 328 | func scanRepo(row *sql.Row) (Repo, error) { |
| 329 | var r Repo |
| 330 | var created, pushed string |
| 331 | var owner sql.NullString |
| 332 | if err := row.Scan(&r.ID, &r.OwnerKind, &r.OwnerID, &owner, &r.Name, &r.Description, &r.DefaultBranch, &r.Visibility, &created, &r.LogoMIME, &r.ReleaseVisibility, &pushed); err != nil { |
| 333 | if errors.Is(err, sql.ErrNoRows) { |
| 334 | return Repo{}, errNotFound |
| 335 | } |
| 336 | return Repo{}, err |
| 337 | } |
| 338 | r.OwnerPath = owner.String |
| 339 | r.CreatedAt = parseTime(created) |
| 340 | if pushed != "" { |
| 341 | r.PushedAt = parseTime(pushed) |
| 342 | } |
| 343 | return r, nil |
| 344 | } |
| 345 | |
| 346 | // UpdateRepo wijzigt beschrijving en zichtbaarheid. |
| 347 | func (s *Store) UpdateRepo(ctx context.Context, id int64, description, visibility string) error { |
| 348 | if visibility != "public" && visibility != "private" { |
| 349 | return i18n.E("err.visibility") |
| 350 | } |
| 351 | _, err := s.db.ExecContext(ctx, `UPDATE repos SET description = ?, visibility = ? WHERE id = ?`, strings.TrimSpace(description), visibility, id) |
| 352 | return err |
| 353 | } |
| 354 | |
| 355 | // DeleteRepo verwijdert de metadata. Bestanden verwijdert de aanroeper. |
| 356 | func (s *Store) DeleteRepo(ctx context.Context, id int64) error { |
| 357 | _, err := s.db.ExecContext(ctx, `DELETE FROM repos WHERE id = ?`, id) |
| 358 | return err |
| 359 | } |
| 360 | |
| 361 | // ListRepoIDs geeft repositories die deze gebruiker mag zien: |
| 362 | // openbare repositories, eigen repositories, repositories van een groep |
| 363 | // waarvan hij lid is, en repositories met een rechtstreeks lidmaatschap. |
| 364 | // Installatiebeheer voegt daar niets aan toe. |
| 365 | func (s *Store) ListRepoIDs(ctx context.Context, userID int64) ([]int64, error) { |
| 366 | q := `SELECT id FROM repos WHERE visibility = 'public'` |
| 367 | args := []any{} |
| 368 | if userID != 0 { |
| 369 | q = `SELECT DISTINCT id FROM repos WHERE visibility = 'public' |
| 370 | OR (owner_kind = 'user' AND owner_id = ?) |
| 371 | OR (owner_kind = 'group' AND owner_id IN (SELECT group_id FROM group_members WHERE user_id = ?)) |
| 372 | OR id IN (SELECT repo_id FROM repo_members WHERE user_id = ?)` |
| 373 | args = []any{userID, userID, userID} |
| 374 | } |
| 375 | q += ` ORDER BY id DESC` |
| 376 | rows, err := s.db.QueryContext(ctx, q, args...) |
| 377 | if err != nil { |
| 378 | return nil, err |
| 379 | } |
| 380 | defer rows.Close() |
| 381 | var ids []int64 |
| 382 | for rows.Next() { |
| 383 | var id int64 |
| 384 | if err := rows.Scan(&id); err != nil { |
| 385 | return nil, err |
| 386 | } |
| 387 | ids = append(ids, id) |
| 388 | } |
| 389 | return ids, rows.Err() |
| 390 | } |
| 391 | |
| 392 | // pushOrder: laatste push bovenaan, repos zonder push onderaan, daarna op naam. |
| 393 | const pushOrder = `(r.pushed_at = ''), r.pushed_at DESC, r.name COLLATE NOCASE, r.id` |
| 394 | |
| 395 | // ReposByOwner geeft de repositories van een gebruiker, laatste push eerst. |
| 396 | func (s *Store) ReposByOwner(ctx context.Context, userID int64) ([]Repo, error) { |
| 397 | rows, err := s.db.QueryContext(ctx, repoSelect+` WHERE r.owner_kind = 'user' AND r.owner_id = ? ORDER BY `+pushOrder, userID) |
| 398 | if err != nil { |
| 399 | return nil, err |
| 400 | } |
| 401 | defer rows.Close() |
| 402 | return scanRepos(rows) |
| 403 | } |
| 404 | |
| 405 | // CountRepos telt repositories van een eigenaar. publicOnly slaat privé over. |
| 406 | func (s *Store) CountRepos(ctx context.Context, ownerKind string, ownerID int64, publicOnly bool) (int, error) { |
| 407 | q := `SELECT COUNT(*) FROM repos WHERE owner_kind = ? AND owner_id = ?` |
| 408 | if publicOnly { |
| 409 | q += ` AND visibility = 'public'` |
| 410 | } |
| 411 | var n int |
| 412 | err := s.db.QueryRowContext(ctx, q, ownerKind, ownerID).Scan(&n) |
| 413 | return n, err |
| 414 | } |
| 415 | |
| 416 | // ReposByGroup geeft de repositories van een groep. |
| 417 | func (s *Store) ReposByGroup(ctx context.Context, groupID int64) ([]Repo, error) { |
| 418 | rows, err := s.db.QueryContext(ctx, repoSelect+` WHERE r.owner_kind = 'group' AND r.owner_id = ? ORDER BY `+pushOrder, groupID) |
| 419 | if err != nil { |
| 420 | return nil, err |
| 421 | } |
| 422 | defer rows.Close() |
| 423 | return scanRepos(rows) |
| 424 | } |
| 425 | |
| 426 | // ReposByIDs laadt een lijst. |
| 427 | func (s *Store) ReposByIDs(ctx context.Context, ids []int64) ([]Repo, error) { |
| 428 | var out []Repo |
| 429 | for _, id := range ids { |
| 430 | r, err := s.RepoByID(ctx, id) |
| 431 | if err != nil { |
| 432 | return nil, err |
| 433 | } |
| 434 | out = append(out, r) |
| 435 | } |
| 436 | return out, nil |
| 437 | } |
| 438 | |
| 439 | func scanRepos(rows *sql.Rows) ([]Repo, error) { |
| 440 | var out []Repo |
| 441 | for rows.Next() { |
| 442 | var r Repo |
| 443 | var created, pushed string |
| 444 | var owner sql.NullString |
| 445 | if err := rows.Scan(&r.ID, &r.OwnerKind, &r.OwnerID, &owner, &r.Name, &r.Description, &r.DefaultBranch, &r.Visibility, &created, &r.LogoMIME, &r.ReleaseVisibility, &pushed); err != nil { |
| 446 | return nil, err |
| 447 | } |
| 448 | r.OwnerPath = owner.String |
| 449 | r.CreatedAt = parseTime(created) |
| 450 | if pushed != "" { |
| 451 | r.PushedAt = parseTime(pushed) |
| 452 | } |
| 453 | if pushed != "" { |
| 454 | r.PushedAt = parseTime(pushed) |
| 455 | } |
| 456 | out = append(out, r) |
| 457 | } |
| 458 | return out, rows.Err() |
| 459 | } |
| 460 | |
| 461 | // AllRepos is voor het verversen van hooks bij het starten. |
| 462 | func (s *Store) AllRepos(ctx context.Context) ([]Repo, error) { |
| 463 | rows, err := s.db.QueryContext(ctx, repoSelect+` ORDER BY r.id`) |
| 464 | if err != nil { |
| 465 | return nil, err |
| 466 | } |
| 467 | defer rows.Close() |
| 468 | return scanRepos(rows) |
| 469 | } |
| 470 | |
| 471 | // EffectiveRole past de rolregels toe op een repository. |
| 472 | func (s *Store) EffectiveRole(ctx context.Context, userID, repoID int64) (access.Role, error) { |
| 473 | if userID == 0 { |
| 474 | return access.RoleNone, nil |
| 475 | } |
| 476 | if _, err := s.UserByID(ctx, userID); err != nil { |
| 477 | return access.RoleNone, err |
| 478 | } |
| 479 | repo, err := s.RepoByID(ctx, repoID) |
| 480 | if err != nil { |
| 481 | return access.RoleNone, err |
| 482 | } |
| 483 | in := access.Input{} |
| 484 | if repo.OwnerKind == "user" && repo.OwnerID == userID { |
| 485 | in.UserOwner = true |
| 486 | } |
| 487 | if repo.OwnerKind == "group" { |
| 488 | role, err := s.GroupRole(ctx, repo.OwnerID, userID) |
| 489 | if err != nil { |
| 490 | return access.RoleNone, err |
| 491 | } |
| 492 | in.GroupRole = role |
| 493 | } |
| 494 | var role string |
| 495 | err = s.db.QueryRowContext(ctx, `SELECT role FROM repo_members WHERE repo_id = ? AND user_id = ?`, repoID, userID).Scan(&role) |
| 496 | if err != nil && !errors.Is(err, sql.ErrNoRows) { |
| 497 | return access.RoleNone, err |
| 498 | } |
| 499 | if err == nil { |
| 500 | in.RepoRole = access.Role(role) |
| 501 | } |
| 502 | return access.Resolve(in), nil |
| 503 | } |
| 504 | |
| 505 | // Member is een lid of een uitnodiging op gebruikersnaam. |
| 506 | type Member struct { |
| 507 | UserID int64 |
| 508 | Username string |
| 509 | AvatarURL string |
| 510 | Role access.Role |
| 511 | Pending bool |
| 512 | } |
| 513 | |
| 514 | // SetMember zet een lidmaatschap. Een onbekende naam blijft wachten tot de eerste login. |
| 515 | func (s *Store) SetMember(ctx context.Context, scopeKind string, scopeID int64, username string, role access.Role) error { |
| 516 | if scopeKind != "repo" && scopeKind != "group" { |
| 517 | return i18n.E("err.scope") |
| 518 | } |
| 519 | if _, err := access.ParseRole(string(role)); err != nil { |
| 520 | return err |
| 521 | } |
| 522 | username = strings.ToLower(strings.TrimSpace(username)) |
| 523 | if err := names.ValidUsername(username); err != nil { |
| 524 | return err |
| 525 | } |
| 526 | if scopeKind == "group" && role != access.RoleMaintainer { |
| 527 | g, err := s.GroupByID(ctx, scopeID) |
| 528 | if err != nil { |
| 529 | return err |
| 530 | } |
| 531 | owner, err := s.UserByID(ctx, g.OwnerID) |
| 532 | if err == nil && owner.Username == username { |
| 533 | return i18n.E("err.ownerRole") |
| 534 | } |
| 535 | } |
| 536 | u, err := s.UserByUsername(ctx, username) |
| 537 | if IsNotFound(err) { |
| 538 | _, err = s.db.ExecContext(ctx, `INSERT INTO pending_members (scope_kind, scope_id, username, role) VALUES (?, ?, ?, ?) |
| 539 | ON CONFLICT(scope_kind, scope_id, username) DO UPDATE SET role = excluded.role`, scopeKind, scopeID, username, string(role)) |
| 540 | return err |
| 541 | } |
| 542 | if err != nil { |
| 543 | return err |
| 544 | } |
| 545 | table, col := "repo_members", "repo_id" |
| 546 | if scopeKind == "group" { |
| 547 | table, col = "group_members", "group_id" |
| 548 | } |
| 549 | _, err = s.db.ExecContext(ctx, `INSERT INTO `+table+` (`+col+`, user_id, role) VALUES (?, ?, ?) |
| 550 | ON CONFLICT(`+col+`, user_id) DO UPDATE SET role = excluded.role`, scopeID, u.ID, string(role)) |
| 551 | return err |
| 552 | } |
| 553 | |
| 554 | // RemoveMember haalt een lid of een uitnodiging weg. |
| 555 | func (s *Store) RemoveMember(ctx context.Context, scopeKind string, scopeID int64, username string) error { |
| 556 | username = strings.ToLower(strings.TrimSpace(username)) |
| 557 | _, err := s.db.ExecContext(ctx, `DELETE FROM pending_members WHERE scope_kind = ? AND scope_id = ? AND username = ?`, scopeKind, scopeID, username) |
| 558 | if err != nil { |
| 559 | return err |
| 560 | } |
| 561 | u, err := s.UserByUsername(ctx, username) |
| 562 | if IsNotFound(err) { |
| 563 | return nil |
| 564 | } |
| 565 | if err != nil { |
| 566 | return err |
| 567 | } |
| 568 | if scopeKind == "group" { |
| 569 | g, err := s.GroupByID(ctx, scopeID) |
| 570 | if err != nil { |
| 571 | return err |
| 572 | } |
| 573 | if u.ID == g.OwnerID { |
| 574 | return i18n.E("err.ownerRole") |
| 575 | } |
| 576 | _, err = s.db.ExecContext(ctx, `DELETE FROM group_members WHERE group_id = ? AND user_id = ?`, scopeID, u.ID) |
| 577 | return err |
| 578 | } |
| 579 | _, err = s.db.ExecContext(ctx, `DELETE FROM repo_members WHERE repo_id = ? AND user_id = ?`, scopeID, u.ID) |
| 580 | return err |
| 581 | } |
| 582 | |
| 583 | // ListMembers geeft leden van een repository of groep, plus open uitnodigingen. |
| 584 | func (s *Store) ListMembers(ctx context.Context, scopeKind string, scopeID int64) ([]Member, error) { |
| 585 | var q string |
| 586 | if scopeKind == "group" { |
| 587 | q = `SELECT u.id, u.username, u.avatar_url, m.role FROM group_members m JOIN users u ON u.id = m.user_id WHERE m.group_id = ? ORDER BY u.username` |
| 588 | } else { |
| 589 | q = `SELECT u.id, u.username, u.avatar_url, m.role FROM repo_members m JOIN users u ON u.id = m.user_id WHERE m.repo_id = ? ORDER BY u.username` |
| 590 | } |
| 591 | rows, err := s.db.QueryContext(ctx, q, scopeID) |
| 592 | if err != nil { |
| 593 | return nil, err |
| 594 | } |
| 595 | defer rows.Close() |
| 596 | var out []Member |
| 597 | for rows.Next() { |
| 598 | var m Member |
| 599 | var role string |
| 600 | if err := rows.Scan(&m.UserID, &m.Username, &m.AvatarURL, &role); err != nil { |
| 601 | return nil, err |
| 602 | } |
| 603 | m.Role = access.Role(role) |
| 604 | out = append(out, m) |
| 605 | } |
| 606 | if err := rows.Err(); err != nil { |
| 607 | return nil, err |
| 608 | } |
| 609 | prows, err := s.db.QueryContext(ctx, `SELECT username, role FROM pending_members WHERE scope_kind = ? AND scope_id = ? ORDER BY username`, scopeKind, scopeID) |
| 610 | if err != nil { |
| 611 | return nil, err |
| 612 | } |
| 613 | defer prows.Close() |
| 614 | for prows.Next() { |
| 615 | var m Member |
| 616 | var role string |
| 617 | if err := prows.Scan(&m.Username, &role); err != nil { |
| 618 | return nil, err |
| 619 | } |
| 620 | m.Role = access.Role(role) |
| 621 | m.Pending = true |
| 622 | out = append(out, m) |
| 623 | } |
| 624 | return out, prows.Err() |
| 625 | } |
| 626 | |
| 627 | // ProtectedPatterns geeft de beschermde branchpatronen. |
| 628 | func (s *Store) ProtectedPatterns(ctx context.Context, repoID int64) ([]string, error) { |
| 629 | rows, err := s.db.QueryContext(ctx, `SELECT pattern FROM protected_branches WHERE repo_id = ? ORDER BY pattern`, repoID) |
| 630 | if err != nil { |
| 631 | return nil, err |
| 632 | } |
| 633 | defer rows.Close() |
| 634 | var out []string |
| 635 | for rows.Next() { |
| 636 | var p string |
| 637 | if err := rows.Scan(&p); err != nil { |
| 638 | return nil, err |
| 639 | } |
| 640 | out = append(out, p) |
| 641 | } |
| 642 | return out, rows.Err() |
| 643 | } |
| 644 | |
| 645 | // AddProtected voegt een patroon toe. |
| 646 | func (s *Store) AddProtected(ctx context.Context, repoID int64, pattern string) error { |
| 647 | pattern = strings.TrimSpace(pattern) |
| 648 | if pattern == "" || strings.Contains(pattern, " ") || strings.Contains(pattern, "..") { |
| 649 | return i18n.E("err.pattern", pattern) |
| 650 | } |
| 651 | _, err := s.db.ExecContext(ctx, `INSERT INTO protected_branches (repo_id, pattern) VALUES (?, ?)`, repoID, pattern) |
| 652 | if err != nil && strings.Contains(err.Error(), "UNIQUE") { |
| 653 | return nil |
| 654 | } |
| 655 | return err |
| 656 | } |
| 657 | |
| 658 | // RemoveProtected haalt een patroon weg. |
| 659 | func (s *Store) RemoveProtected(ctx context.Context, repoID int64, pattern string) error { |
| 660 | _, err := s.db.ExecContext(ctx, `DELETE FROM protected_branches WHERE repo_id = ? AND pattern = ?`, repoID, pattern) |
| 661 | return err |
| 662 | } |
| 663 | |
| 664 | // ProtectedTagPatterns geeft de beschermde tagpatronen. |
| 665 | func (s *Store) ProtectedTagPatterns(ctx context.Context, repoID int64) ([]string, error) { |
| 666 | rows, err := s.db.QueryContext(ctx, `SELECT pattern FROM protected_tags WHERE repo_id = ? ORDER BY pattern`, repoID) |
| 667 | if err != nil { |
| 668 | return nil, err |
| 669 | } |
| 670 | defer rows.Close() |
| 671 | var out []string |
| 672 | for rows.Next() { |
| 673 | var p string |
| 674 | if err := rows.Scan(&p); err != nil { |
| 675 | return nil, err |
| 676 | } |
| 677 | out = append(out, p) |
| 678 | } |
| 679 | return out, rows.Err() |
| 680 | } |
| 681 | |
| 682 | // AddProtectedTag voegt een tagpatroon toe. |
| 683 | func (s *Store) AddProtectedTag(ctx context.Context, repoID int64, pattern string) error { |
| 684 | pattern = strings.TrimSpace(pattern) |
| 685 | if pattern == "" || strings.Contains(pattern, " ") || strings.Contains(pattern, "..") { |
| 686 | return i18n.E("err.pattern", pattern) |
| 687 | } |
| 688 | _, err := s.db.ExecContext(ctx, `INSERT INTO protected_tags (repo_id, pattern) VALUES (?, ?)`, repoID, pattern) |
| 689 | if err != nil && strings.Contains(err.Error(), "UNIQUE") { |
| 690 | return nil |
| 691 | } |
| 692 | return err |
| 693 | } |
| 694 | |
| 695 | // RemoveProtectedTag haalt een tagpatroon weg. |
| 696 | func (s *Store) RemoveProtectedTag(ctx context.Context, repoID int64, pattern string) error { |
| 697 | _, err := s.db.ExecContext(ctx, `DELETE FROM protected_tags WHERE repo_id = ? AND pattern = ?`, repoID, pattern) |
| 698 | return err |
| 699 | } |
| 700 | |
| 701 | // SecretMeta is een geheim zonder de waarde. |
| 702 | type SecretMeta struct { |
| 703 | Name string |
| 704 | Protected bool |
| 705 | Invisible bool |
| 706 | // Expand: $NAAM en ${NAAM} in de waarde worden in de runner vervangen. |
| 707 | Expand bool |
| 708 | CreatedAt time.Time |
| 709 | } |
| 710 | |
| 711 | // PutSecret bewaart een versleutelde waarde. De naam is een omgevingsvariabele. |
| 712 | // Bij een bestaande naam blijven waarde en protected bijgewerkt; invisible wordt nooit gewist. |
| 713 | func (s *Store) PutSecret(ctx context.Context, scopeKind string, scopeID int64, name, value string, protected bool) error { |
| 714 | if scopeKind != "repo" && scopeKind != "group" { |
| 715 | return i18n.E("err.scope") |
| 716 | } |
| 717 | if err := access.ValidSecretName(name); err != nil { |
| 718 | return err |
| 719 | } |
| 720 | if strings.TrimSpace(value) == "" { |
| 721 | return i18n.E("err.emptyValue") |
| 722 | } |
| 723 | nonce, ct, err := s.encrypt(value) |
| 724 | if err != nil { |
| 725 | return err |
| 726 | } |
| 727 | _, err = s.db.ExecContext(ctx, `INSERT INTO secrets (scope_kind, scope_id, name, ciphertext, nonce, protected, created_at) VALUES (?, ?, ?, ?, ?, ?, ?) |
| 728 | ON CONFLICT(scope_kind, scope_id, name) DO UPDATE SET ciphertext = excluded.ciphertext, nonce = excluded.nonce, protected = excluded.protected`, |
| 729 | scopeKind, scopeID, name, ct, nonce, boolInt(protected), now()) |
| 730 | return err |
| 731 | } |
| 732 | |
| 733 | // DeleteSecret verwijdert een geheim. |
| 734 | func (s *Store) DeleteSecret(ctx context.Context, scopeKind string, scopeID int64, name string) error { |
| 735 | res, err := s.db.ExecContext(ctx, `DELETE FROM secrets WHERE scope_kind = ? AND scope_id = ? AND name = ?`, scopeKind, scopeID, name) |
| 736 | if err != nil { |
| 737 | return err |
| 738 | } |
| 739 | n, _ := res.RowsAffected() |
| 740 | if n == 0 { |
| 741 | return errNotFound |
| 742 | } |
| 743 | return nil |
| 744 | } |
| 745 | |
| 746 | // CreateSecret voegt een geheim toe. Een bestaande naam wordt niet overschreven. |
| 747 | func (s *Store) CreateSecret(ctx context.Context, scopeKind string, scopeID int64, name, value string, protected, invisible bool) error { |
| 748 | nonce, ct, err := s.secretCipher(scopeKind, name, value) |
| 749 | if err != nil { |
| 750 | return err |
| 751 | } |
| 752 | _, err = s.db.ExecContext(ctx, `INSERT INTO secrets (scope_kind, scope_id, name, ciphertext, nonce, protected, invisible, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, |
| 753 | scopeKind, scopeID, name, ct, nonce, boolInt(protected), boolInt(invisible), now()) |
| 754 | if err != nil && strings.Contains(err.Error(), "UNIQUE") { |
| 755 | return i18n.E("err.secretExists", name) |
| 756 | } |
| 757 | return err |
| 758 | } |
| 759 | |
| 760 | // UpdateSecret wijzigt waarde en protected van een zichtbaar geheim. |
| 761 | func (s *Store) UpdateSecret(ctx context.Context, scopeKind string, scopeID int64, name, value string, protected bool) error { |
| 762 | return s.writeSecret(ctx, scopeKind, scopeID, name, value, protected, false) |
| 763 | } |
| 764 | |
| 765 | // ReplaceSecret zet een nieuwe waarde op een onzichtbaar geheim. De vlag blijft aan. |
| 766 | func (s *Store) ReplaceSecret(ctx context.Context, scopeKind string, scopeID int64, name, value string, protected bool) error { |
| 767 | return s.writeSecret(ctx, scopeKind, scopeID, name, value, protected, true) |
| 768 | } |
| 769 | |
| 770 | func (s *Store) writeSecret(ctx context.Context, scopeKind string, scopeID int64, name, value string, protected, invisible bool) error { |
| 771 | nonce, ct, err := s.secretCipher(scopeKind, name, value) |
| 772 | if err != nil { |
| 773 | return err |
| 774 | } |
| 775 | res, err := s.db.ExecContext(ctx, `UPDATE secrets SET ciphertext = ?, nonce = ?, protected = ? WHERE scope_kind = ? AND scope_id = ? AND name = ? AND invisible = ?`, |
| 776 | ct, nonce, boolInt(protected), scopeKind, scopeID, name, boolInt(invisible)) |
| 777 | if err != nil { |
| 778 | return err |
| 779 | } |
| 780 | n, _ := res.RowsAffected() |
| 781 | if n == 1 { |
| 782 | return nil |
| 783 | } |
| 784 | return s.secretMiss(ctx, scopeKind, scopeID, name, invisible) |
| 785 | } |
| 786 | |
| 787 | func (s *Store) secretCipher(scopeKind, name, value string) (nonce, ct []byte, err error) { |
| 788 | if scopeKind != "repo" && scopeKind != "group" { |
| 789 | return nil, nil, i18n.E("err.scope") |
| 790 | } |
| 791 | if err := access.ValidSecretName(name); err != nil { |
| 792 | return nil, nil, err |
| 793 | } |
| 794 | if strings.TrimSpace(value) == "" { |
| 795 | return nil, nil, i18n.E("err.emptyValue") |
| 796 | } |
| 797 | return s.encrypt(value) |
| 798 | } |
| 799 | |
| 800 | func (s *Store) secretMiss(ctx context.Context, scopeKind string, scopeID int64, name string, wantInvisible bool) error { |
| 801 | var inv int |
| 802 | err := s.db.QueryRowContext(ctx, `SELECT invisible FROM secrets WHERE scope_kind = ? AND scope_id = ? AND name = ?`, scopeKind, scopeID, name).Scan(&inv) |
| 803 | if errors.Is(err, sql.ErrNoRows) { |
| 804 | return errNotFound |
| 805 | } |
| 806 | if err != nil { |
| 807 | return err |
| 808 | } |
| 809 | if wantInvisible && inv == 0 { |
| 810 | return i18n.E("err.secretVisible", name) |
| 811 | } |
| 812 | if !wantInvisible && inv == 1 { |
| 813 | return i18n.E("err.secretHidden", name) |
| 814 | } |
| 815 | return errNotFound |
| 816 | } |
| 817 | |
| 818 | // RevealSecret geeft de waarde van een zichtbaar geheim. |
| 819 | func (s *Store) RevealSecret(ctx context.Context, scopeKind string, scopeID int64, name string) (string, error) { |
| 820 | if scopeKind != "repo" && scopeKind != "group" { |
| 821 | return "", i18n.E("err.scope") |
| 822 | } |
| 823 | var ct, nonce []byte |
| 824 | var inv int |
| 825 | err := s.db.QueryRowContext(ctx, `SELECT ciphertext, nonce, invisible FROM secrets WHERE scope_kind = ? AND scope_id = ? AND name = ?`, scopeKind, scopeID, name).Scan(&ct, &nonce, &inv) |
| 826 | if errors.Is(err, sql.ErrNoRows) { |
| 827 | return "", errNotFound |
| 828 | } |
| 829 | if err != nil { |
| 830 | return "", err |
| 831 | } |
| 832 | if inv == 1 { |
| 833 | return "", i18n.E("err.secretHidden", name) |
| 834 | } |
| 835 | return s.decrypt(nonce, ct) |
| 836 | } |
| 837 | |
| 838 | // ListSecrets geeft namen, niet de waarden. |
| 839 | func (s *Store) ListSecrets(ctx context.Context, scopeKind string, scopeID int64) ([]SecretMeta, error) { |
| 840 | rows, err := s.db.QueryContext(ctx, `SELECT name, protected, invisible, expand, created_at FROM secrets WHERE scope_kind = ? AND scope_id = ? ORDER BY name`, scopeKind, scopeID) |
| 841 | if err != nil { |
| 842 | return nil, err |
| 843 | } |
| 844 | defer rows.Close() |
| 845 | var out []SecretMeta |
| 846 | for rows.Next() { |
| 847 | var m SecretMeta |
| 848 | var p, inv, exp int |
| 849 | var created string |
| 850 | if err := rows.Scan(&m.Name, &p, &inv, &exp, &created); err != nil { |
| 851 | return nil, err |
| 852 | } |
| 853 | m.Protected = p == 1 |
| 854 | m.Invisible = inv == 1 |
| 855 | m.Expand = exp == 1 |
| 856 | m.CreatedAt = parseTime(created) |
| 857 | out = append(out, m) |
| 858 | } |
| 859 | return out, rows.Err() |
| 860 | } |
| 861 | |
| 862 | // SecretValues ontcijfert geheimen voor een job of webhook. |
| 863 | // refProtected bepaalt of beschermde geheimen meegaan. |
| 864 | func (s *Store) SecretValues(ctx context.Context, scopeKind string, scopeID int64, refProtected bool) (map[string]string, error) { |
| 865 | rows, err := s.db.QueryContext(ctx, `SELECT name, ciphertext, nonce, protected FROM secrets WHERE scope_kind = ? AND scope_id = ?`, scopeKind, scopeID) |
| 866 | if err != nil { |
| 867 | return nil, err |
| 868 | } |
| 869 | defer rows.Close() |
| 870 | out := map[string]string{} |
| 871 | for rows.Next() { |
| 872 | var name string |
| 873 | var ct, nonce []byte |
| 874 | var p int |
| 875 | if err := rows.Scan(&name, &ct, &nonce, &p); err != nil { |
| 876 | return nil, err |
| 877 | } |
| 878 | if !access.InjectSecret(p == 1, refProtected) { |
| 879 | continue |
| 880 | } |
| 881 | val, err := s.decrypt(nonce, ct) |
| 882 | if err != nil { |
| 883 | return nil, err |
| 884 | } |
| 885 | out[name] = val |
| 886 | } |
| 887 | return out, rows.Err() |
| 888 | } |
| 889 | |
| 890 | // TouchPushed zet het moment van de laatste push. |
| 891 | func (s *Store) TouchPushed(ctx context.Context, repoID int64, at time.Time) error { |
| 892 | _, err := s.db.ExecContext(ctx, `UPDATE repos SET pushed_at = ? WHERE id = ? AND (pushed_at = '' OR pushed_at < ?)`, |
| 893 | at.UTC().Format(time.RFC3339), repoID, at.UTC().Format(time.RFC3339)) |
| 894 | return err |
| 895 | } |
| 896 | |
| 897 | // SortReposByPush sorteert zoals pushOrder: laatste push eerst, zonder push |
| 898 | // onderaan, daarna op naam. |
| 899 | func SortReposByPush(list []Repo) { |
| 900 | sort.SliceStable(list, func(i, j int) bool { |
| 901 | a, b := list[i], list[j] |
| 902 | if a.PushedAt.IsZero() != b.PushedAt.IsZero() { |
| 903 | return !a.PushedAt.IsZero() |
| 904 | } |
| 905 | if !a.PushedAt.Equal(b.PushedAt) { |
| 906 | return a.PushedAt.After(b.PushedAt) |
| 907 | } |
| 908 | if x, y := strings.ToLower(a.FullPath()), strings.ToLower(b.FullPath()); x != y { |
| 909 | return x < y |
| 910 | } |
| 911 | return a.ID < b.ID |
| 912 | }) |
| 913 | } |
| 914 | |
| 915 | // GroupPushTimes geeft per groep de laatste push in een van haar repos. |
| 916 | // Groepen zonder push ontbreken in de map. |
| 917 | func (s *Store) GroupPushTimes(ctx context.Context) (map[int64]time.Time, error) { |
| 918 | rows, err := s.db.QueryContext(ctx, `SELECT owner_id, MAX(pushed_at) FROM repos WHERE owner_kind = 'group' AND pushed_at <> '' GROUP BY owner_id`) |
| 919 | if err != nil { |
| 920 | return nil, err |
| 921 | } |
| 922 | defer rows.Close() |
| 923 | out := map[int64]time.Time{} |
| 924 | for rows.Next() { |
| 925 | var id int64 |
| 926 | var at string |
| 927 | if err := rows.Scan(&id, &at); err != nil { |
| 928 | return nil, err |
| 929 | } |
| 930 | out[id] = parseTime(at) |
| 931 | } |
| 932 | return out, rows.Err() |
| 933 | } |
| 934 | |
| 935 | // SortGroupsByPush: laatste push eerst, groepen zonder push onderaan, dan op naam. |
| 936 | func SortGroupsByPush(list []Group, at map[int64]time.Time) { |
| 937 | sort.SliceStable(list, func(i, j int) bool { |
| 938 | a, b := at[list[i].ID], at[list[j].ID] |
| 939 | if a.IsZero() != b.IsZero() { |
| 940 | return !a.IsZero() |
| 941 | } |
| 942 | if !a.Equal(b) { |
| 943 | return a.After(b) |
| 944 | } |
| 945 | return strings.ToLower(list[i].Name) < strings.ToLower(list[j].Name) |
| 946 | }) |
| 947 | } |
| 948 | |
| 949 | // SetSecretExpand zet of de waarde variabelen uitbreidt. |
| 950 | func (s *Store) SetSecretExpand(ctx context.Context, scopeKind string, scopeID int64, name string, on bool) error { |
| 951 | _, err := s.db.ExecContext(ctx, `UPDATE secrets SET expand = ? WHERE scope_kind = ? AND scope_id = ? AND name = ?`, boolInt(on), scopeKind, scopeID, name) |
| 952 | return err |
| 953 | } |
| 954 | |
| 955 | // SecretExpandFlags geeft per geheimnaam of expand aan staat. |
| 956 | func (s *Store) SecretExpandFlags(ctx context.Context, scopeKind string, scopeID int64) (map[string]bool, error) { |
| 957 | rows, err := s.db.QueryContext(ctx, `SELECT name, expand FROM secrets WHERE scope_kind = ? AND scope_id = ?`, scopeKind, scopeID) |
| 958 | if err != nil { |
| 959 | return nil, err |
| 960 | } |
| 961 | defer rows.Close() |
| 962 | out := map[string]bool{} |
| 963 | for rows.Next() { |
| 964 | var name string |
| 965 | var e int |
| 966 | if err := rows.Scan(&name, &e); err != nil { |
| 967 | return nil, err |
| 968 | } |
| 969 | out[name] = e == 1 |
| 970 | } |
| 971 | return out, rows.Err() |
| 972 | } |