2222import com .gitblit .utils .StringUtils ;
2323import com .google .gerrit .extensions .client .ProjectState ;
2424import com .google .gerrit .reviewdb .client .Project .NameKey ;
25+ import com .google .gerrit .server .CurrentUser ;
26+ import com .google .gerrit .server .permissions .PermissionBackend ;
27+ import com .google .gerrit .server .permissions .ProjectPermission ;
2528import com .google .gerrit .server .project .NoSuchProjectException ;
2629import com .google .gerrit .server .project .ProjectControl ;
2730import com .google .gerrit .server .project .ProjectControl .Factory ;
31+ import com .google .inject .Provider ;
2832import java .util .HashSet ;
2933import java .util .Set ;
3034
@@ -46,24 +50,34 @@ public class GerritToGitBlitUserModel extends UserModel {
4650 public final Set <TeamModel > teams = new HashSet <TeamModel >();
4751
4852 private final transient ProjectControl .Factory projectControlFactory ;
53+ private final transient Provider <CurrentUser > userProvider ;
54+ private final transient PermissionBackend permissionBackend ;
4955
5056 // non-persisted fields
5157 public boolean isAuthenticated ;
5258
5359 public GerritToGitBlitUserModel (String username ) {
54- this (username , null );
60+ this (username , null , null , null );
5561 }
5662
57- public GerritToGitBlitUserModel (String username , ProjectControl .Factory projectControlFactory ) {
63+ public GerritToGitBlitUserModel (
64+ String username ,
65+ ProjectControl .Factory projectControlFactory ,
66+ Provider <CurrentUser > userProvider ,
67+ PermissionBackend persmissionBackend ) {
5868 super (username );
5969 this .username = username ;
6070 this .isAuthenticated = true ;
6171 this .projectControlFactory = projectControlFactory ;
72+ this .userProvider = userProvider ;
73+ this .permissionBackend = persmissionBackend ;
6274 }
6375
6476 public GerritToGitBlitUserModel (ProjectControl .Factory projectControlFactory ) {
6577 super (ANONYMOUS_USER );
6678 this .projectControlFactory = projectControlFactory ;
79+ this .userProvider = null ;
80+ this .permissionBackend = null ;
6781 }
6882
6983 @ Deprecated
@@ -88,8 +102,8 @@ protected boolean canAccess(
88102 boolean result = false ;
89103
90104 try {
91- ProjectControl control =
92- projectControlFactory .controlFor (new NameKey ( getRepositoryName ( repository . name )) );
105+ NameKey project = new NameKey ( getRepositoryName ( repository . name ));
106+ ProjectControl control = projectControlFactory .controlFor (project );
93107
94108 if (control == null ) {
95109 return false ;
@@ -99,9 +113,15 @@ protected boolean canAccess(
99113 case VIEW :
100114 return !control .getProject ().getState ().equals (ProjectState .HIDDEN );
101115 case CLONE :
102- return control .canRunUploadPack ();
116+ return permissionBackend
117+ .user (userProvider )
118+ .project (project )
119+ .testOrFalse (ProjectPermission .RUN_UPLOAD_PACK );
103120 case PUSH :
104- return control .canRunReceivePack ();
121+ permissionBackend
122+ .user (userProvider )
123+ .project (project )
124+ .testOrFalse (ProjectPermission .RUN_RECEIVE_PACK );
105125 default :
106126 return true ;
107127 }
@@ -199,6 +219,6 @@ public int compareTo(UserModel o) {
199219 }
200220
201221 public static UserModel getAnonymous (Factory projectControl ) {
202- return new GerritToGitBlitUserModel (ANONYMOUS_USER , projectControl );
222+ return new GerritToGitBlitUserModel (ANONYMOUS_USER , projectControl , null , null );
203223 }
204224}
0 commit comments