@@ -36,18 +36,27 @@ func NewAuthorizer() Authorizer {
3636func (a * authorizer ) Authorize (ctx context.Context , nameStr string , action Action ) error {
3737 auth := auth .ArtifactsAuthentication .Require (ctx )
3838
39- if action == ActionWrite &&
40- (auth .CurrentCustomerOrgID () != nil ||
41- auth .CurrentUserRole () == nil ||
42- * auth .CurrentUserRole () == types .UserRoleReadOnly ) {
43- return ErrAccessDenied
39+ if action == ActionWrite {
40+ if auth .CurrentCustomerOrgID () != nil {
41+ return NewErrAccessDenied ("customer user can not perform write action" )
42+ }
43+
44+ if auth .CurrentUserRole () == nil {
45+ return NewErrAccessDenied ("user with no role can not perform write action" )
46+ }
47+
48+ if * auth .CurrentUserRole () == types .UserRoleReadOnly {
49+ return NewErrAccessDenied ("read-only user can not perform write action" )
50+ }
4451 }
4552
4653 org := auth .CurrentOrg ()
4754 if name , err := name .Parse (nameStr ); err != nil {
4855 return err
49- } else if org .Slug == nil || * org .Slug != name .OrgName {
50- return ErrAccessDenied
56+ } else if org .Slug == nil {
57+ return NewErrAccessDenied ("organization has no slug" )
58+ } else if * org .Slug != name .OrgName {
59+ return NewErrAccessDenied ("organization slug does not match reference" )
5160 }
5261
5362 return nil
@@ -57,18 +66,27 @@ func (a *authorizer) Authorize(ctx context.Context, nameStr string, action Actio
5766func (a * authorizer ) AuthorizeReference (ctx context.Context , nameStr string , reference string , action Action ) error {
5867 auth := auth .ArtifactsAuthentication .Require (ctx )
5968
60- if action == ActionWrite &&
61- (auth .CurrentCustomerOrgID () != nil ||
62- auth .CurrentUserRole () == nil ||
63- * auth .CurrentUserRole () == types .UserRoleReadOnly ) {
64- return ErrAccessDenied
69+ if action == ActionWrite {
70+ if auth .CurrentCustomerOrgID () != nil {
71+ return NewErrAccessDenied ("customer user can not perform write action" )
72+ }
73+
74+ if auth .CurrentUserRole () == nil {
75+ return NewErrAccessDenied ("user with no role can not perform write action" )
76+ }
77+
78+ if * auth .CurrentUserRole () == types .UserRoleReadOnly {
79+ return NewErrAccessDenied ("read-only user can not perform write action" )
80+ }
6581 }
6682
6783 org := auth .CurrentOrg ()
6884 if name , err := name .Parse (nameStr ); err != nil {
6985 return err
70- } else if org .Slug == nil || * org .Slug != name .OrgName {
71- return ErrAccessDenied
86+ } else if org .Slug == nil {
87+ return NewErrAccessDenied ("organization has no slug" )
88+ } else if * org .Slug != name .OrgName {
89+ return NewErrAccessDenied ("organization slug does not match reference" )
7290 } else if action != ActionWrite && auth .CurrentCustomerOrgID () != nil {
7391 if org .HasFeature (types .FeatureLicensing ) {
7492 err := db .CheckLicenseForArtifact (ctx ,
@@ -79,7 +97,7 @@ func (a *authorizer) AuthorizeReference(ctx context.Context, nameStr string, ref
7997 * auth .CurrentOrgID (),
8098 )
8199 if errors .Is (err , apierrors .ErrForbidden ) {
82- return ErrAccessDenied
100+ return NewErrAccessDenied ( "license required" )
83101 } else if err != nil {
84102 return err
85103 }
@@ -93,17 +111,24 @@ func (a *authorizer) AuthorizeReference(ctx context.Context, nameStr string, ref
93111func (a * authorizer ) AuthorizeBlob (ctx context.Context , digest digest.Digest , action Action ) error {
94112 auth := auth .ArtifactsAuthentication .Require (ctx )
95113
96- if action == ActionWrite &&
97- (auth .CurrentCustomerOrgID () != nil ||
98- auth .CurrentUserRole () == nil ||
99- * auth .CurrentUserRole () == types .UserRoleReadOnly ) {
100- return ErrAccessDenied
114+ if action == ActionWrite {
115+ if auth .CurrentCustomerOrgID () != nil {
116+ return NewErrAccessDenied ("customer user can not perform write action" )
117+ }
118+
119+ if auth .CurrentUserRole () == nil {
120+ return NewErrAccessDenied ("user with no role can not perform write action" )
121+ }
122+
123+ if * auth .CurrentUserRole () == types .UserRoleReadOnly {
124+ return NewErrAccessDenied ("read-only user can not perform write action" )
125+ }
101126 }
102127
103128 if auth .CurrentCustomerOrgID () != nil && auth .CurrentOrg ().HasFeature (types .FeatureLicensing ) {
104129 err := db .CheckLicenseForArtifactBlob (ctx , digest .String (), * auth .CurrentCustomerOrgID (), * auth .CurrentOrgID ())
105130 if errors .Is (err , apierrors .ErrForbidden ) {
106- return ErrAccessDenied
131+ return NewErrAccessDenied ( "license required" )
107132 } else if err != nil {
108133 return err
109134 }
0 commit comments