1+ import gitbucket .core .model ._
2+ import gitbucket .core .service .AccountService
13import gitbucket .core .service .SystemSettingsService .SystemSettings
24import gitbucket .gist .controller .GistController
3- import gitbucket .core .plugin .PluginRegistry
5+ import gitbucket .core .plugin ._
46import gitbucket .core .util .Version
57import java .io .File
68import javax .servlet .ServletContext
@@ -15,22 +17,14 @@ class Plugin extends gitbucket.core.plugin.Plugin {
1517 override val description : String = " Provides Gist feature on GitBucket."
1618
1719 override val versions : List [Version ] = List (
20+ Version (1 , 3 ),
1821 Version (1 , 2 ),
1922 Version (1 , 0 )
2023 )
2124
2225 override def initialize (registry : PluginRegistry , context : ServletContext , settings : SystemSettings ): Unit = {
2326 super .initialize(registry, context, settings)
2427
25- // Add Snippet link to the header
26- val path = settings.baseUrl.getOrElse(context.getContextPath)
27- registry.addJavaScript(" .*" ,
28- s """
29- | $$ ('a.brand').after(
30- | $$ ('<span style="float: left; margin-top: 10px;">| <a href=" ${path}/gist" style="color: black;">Snippet</a></span>')
31- |);
32- """ .stripMargin)
33-
3428 // Create gist repository directory
3529 val rootdir = new File (GistRepoDir )
3630 if (! rootdir.exists){
@@ -40,16 +34,39 @@ class Plugin extends gitbucket.core.plugin.Plugin {
4034 println(" -- Gist plug-in initialized --" )
4135 }
4236
37+ override val repositoryRoutings = Seq (
38+ GitRepositoryRouting (" gist/(.+?)/(.+?)\\ .git" , " gist/$1/$2" , new GistRepositoryFilter ())
39+ )
40+
4341 override val controllers = Seq (
4442 " /*" -> new GistController ()
4543 )
4644
47- override val images = Seq (
48- " images/menu-revisions-active.png" -> fromClassPath(" images/menu-revisions-active.png" ),
49- " images/menu-revisions.png" -> fromClassPath(" images/menu-revisions.png" ),
50- " images/snippet.png" -> fromClassPath(" images/snippet.png" )
51- )
45+ override def javaScripts (registry : PluginRegistry , context : ServletContext , settings : SystemSettings ): Seq [(String , String )] = {
46+ // Add Snippet link to the header
47+ val path = settings.baseUrl.getOrElse(context.getContextPath)
48+ Seq (
49+ " .*" -> s """
50+ | $$ ('a.global-header-menu:last').after(
51+ | $$ ('<a href=" ${path}/gist" class="global-header-menu">Gist</a>')
52+ |);
53+ """ .stripMargin
54+ )
55+ }
56+ }
57+
58+ class GistRepositoryFilter extends GitRepositoryFilter with AccountService {
5259
53- override def shutdown (registry : PluginRegistry , context : ServletContext , settings : SystemSettings ): Unit = {
60+ override def filter (path : String , userName : Option [String ], settings : SystemSettings , isUpdating : Boolean )
61+ (implicit session : Session ): Boolean = {
62+ if (isUpdating){
63+ (for {
64+ userName <- userName
65+ account <- getAccountByUserName(userName)
66+ } yield
67+ path.startsWith(" /" + userName + " /" ) || account.isAdmin
68+ ).getOrElse(false )
69+ } else true
5470 }
71+
5572}
0 commit comments