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

permission_adapter.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package casdoor
  15. import (
  16. "github.com/casbin/casibase/util"
  17. "xorm.io/core"
  18. )
  19. type Permission struct {
  20. Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
  21. Name string `xorm:"varchar(100) notnull pk" json:"name"`
  22. CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
  23. DisplayName string `xorm:"varchar(100)" json:"displayName"`
  24. Users []string `xorm:"mediumtext" json:"users"`
  25. Roles []string `xorm:"mediumtext" json:"roles"`
  26. Domains []string `xorm:"mediumtext" json:"domains"`
  27. Model string `xorm:"varchar(100)" json:"model"`
  28. ResourceType string `xorm:"varchar(100)" json:"resourceType"`
  29. Resources []string `xorm:"mediumtext" json:"resources"`
  30. Actions []string `xorm:"mediumtext" json:"actions"`
  31. Effect string `xorm:"varchar(100)" json:"effect"`
  32. IsEnabled bool `json:"isEnabled"`
  33. Submitter string `xorm:"varchar(100)" json:"submitter"`
  34. Approver string `xorm:"varchar(100)" json:"approver"`
  35. ApproveTime string `xorm:"varchar(100)" json:"approveTime"`
  36. State string `xorm:"varchar(100)" json:"state"`
  37. }
  38. func GetPermissions(owner string) ([]*Permission, error) {
  39. permissions := []*Permission{}
  40. err := adapter.Engine.Desc("created_time").Find(&permissions, &Permission{Owner: owner})
  41. if err != nil {
  42. return permissions, err
  43. }
  44. return permissions, nil
  45. }
  46. func getPermission(owner string, name string) (*Permission, error) {
  47. if owner == "" || name == "" {
  48. return nil, nil
  49. }
  50. permission := Permission{Owner: owner, Name: name}
  51. existed, err := adapter.Engine.Get(&permission)
  52. if err != nil {
  53. return &permission, err
  54. }
  55. if existed {
  56. return &permission, nil
  57. } else {
  58. return nil, nil
  59. }
  60. }
  61. func GetPermission(id string) (*Permission, error) {
  62. owner, name := util.GetOwnerAndNameFromId(id)
  63. return getPermission(owner, name)
  64. }
  65. func UpdatePermission(id string, permission *Permission) (bool, error) {
  66. owner, name := util.GetOwnerAndNameFromId(id)
  67. oldPermission, err := getPermission(owner, name)
  68. if err != nil {
  69. return false, err
  70. }
  71. if oldPermission == nil {
  72. return false, nil
  73. }
  74. affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(permission)
  75. if err != nil {
  76. return false, err
  77. }
  78. return affected != 0, nil
  79. }
  80. func AddPermission(permission *Permission) (bool, error) {
  81. affected, err := adapter.Engine.Insert(permission)
  82. if err != nil {
  83. return false, err
  84. }
  85. return affected != 0, nil
  86. }
  87. func DeletePermission(permission *Permission) (bool, error) {
  88. affected, err := adapter.Engine.ID(core.PK{permission.Owner, permission.Name}).Delete(&Permission{})
  89. if err != nil {
  90. return false, err
  91. }
  92. return affected != 0, nil
  93. }