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.

access.go 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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)"` // <user name>/<repo name>
  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. access := &Access{
  48. UserName: strings.ToLower(userName),
  49. RepoName: strings.ToLower(repoName),
  50. }
  51. has, err := orm.Get(access)
  52. if err != nil {
  53. return false, err
  54. } else if !has {
  55. return false, nil
  56. } else if mode > access.Mode {
  57. return false, nil
  58. }
  59. return true, nil
  60. }