You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "strings"
  7. "time"
  8. "github.com/lunny/xorm"
  9. )
  10. // Access types.
  11. const (
  12. AU_READABLE = iota + 1
  13. AU_WRITABLE
  14. )
  15. // Access represents the accessibility of user to repository.
  16. type Access struct {
  17. Id int64
  18. UserName string `xorm:"unique(s)"`
  19. RepoName string `xorm:"unique(s)"`
  20. Mode int `xorm:"unique(s)"`
  21. Created time.Time `xorm:"created"`
  22. }
  23. // AddAccess adds new access record.
  24. func AddAccess(access *Access) error {
  25. access.UserName = strings.ToLower(access.UserName)
  26. access.RepoName = strings.ToLower(access.RepoName)
  27. _, err := orm.Insert(access)
  28. return err
  29. }
  30. // UpdateAccess updates access information.
  31. func UpdateAccess(access *Access) error {
  32. access.UserName = strings.ToLower(access.UserName)
  33. access.RepoName = strings.ToLower(access.RepoName)
  34. _, err := orm.Id(access.Id).Update(access)
  35. return err
  36. }
  37. // UpdateAccess updates access information with session for rolling back.
  38. func UpdateAccessWithSession(sess *xorm.Session, access *Access) error {
  39. if _, err := sess.Id(access.Id).Update(access); err != nil {
  40. sess.Rollback()
  41. return err
  42. }
  43. return nil
  44. }
  45. // HasAccess returns true if someone can read or write to given repository.
  46. func HasAccess(userName, repoName string, mode int) (bool, error) {
  47. return orm.Get(&Access{
  48. Id: 0,
  49. UserName: strings.ToLower(userName),
  50. RepoName: strings.ToLower(repoName),
  51. Mode: mode,
  52. })
  53. }