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.

75 lines
1.8 KiB

10 years ago
10 years ago
  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/go-xorm/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. // DeleteAccess deletes access record.
  38. func DeleteAccess(access *Access) error {
  39. _, err := orm.Delete(access)
  40. return err
  41. }
  42. // UpdateAccess updates access information with session for rolling back.
  43. func UpdateAccessWithSession(sess *xorm.Session, access *Access) error {
  44. if _, err := sess.Id(access.Id).Update(access); err != nil {
  45. sess.Rollback()
  46. return err
  47. }
  48. return nil
  49. }
  50. // HasAccess returns true if someone can read or write to given repository.
  51. func HasAccess(userName, repoName string, mode int) (bool, error) {
  52. access := &Access{
  53. UserName: strings.ToLower(userName),
  54. RepoName: strings.ToLower(repoName),
  55. }
  56. has, err := orm.Get(access)
  57. if err != nil {
  58. return false, err
  59. } else if !has {
  60. return false, nil
  61. } else if mode > access.Mode {
  62. return false, nil
  63. }
  64. return true, nil
  65. }