Skip to content

Commit 9b4a186

Browse files
committed
(refs #19, #24) Real pivate mode
And fix version number in the Plugin class
1 parent 9b92535 commit 9b4a186

File tree

12 files changed

+126
-84
lines changed

12 files changed

+126
-84
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
val Organization = "gitbucket"
22
val Name = "gitbucket-gist-plugin"
3-
val Version = "4.0.0"
3+
val Version = "4.2.0"
44

55
lazy val root = (project in file(".")).enablePlugins(SbtTwirl)
66

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<changeSet>
3+
<addColumn tableName="GIST">
4+
<column name="MODE" type="varchar(10)" nullable="false" defaultValue="PUBLIC"/>
5+
</addColumn>
6+
<sql>UPDATE GIST SET MODE='SECRET' WHERE PRIVATE = TRUE</sql>
7+
<dropColumn tableName="GIST" columnName="PRIVATE"/>
8+
</changeSet>

src/main/scala/Plugin.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ class Plugin extends gitbucket.core.plugin.Plugin {
1919
override val description: String = "Provides Gist feature on GitBucket."
2020

2121
override val versions: List[Version] = List(
22-
new Version("2.0.0",
22+
new Version("2.0.0", // This is mistake in 4.0.0 but it can't be fixed for migration.
2323
new LiquibaseMigration("update/gitbucket-gist_2.0.xml")
24+
),
25+
new Version("4.2.0",
26+
new LiquibaseMigration("update/gitbucket-gist_4.2.xml")
2427
)
2528
)
2629

@@ -33,7 +36,6 @@ class Plugin extends gitbucket.core.plugin.Plugin {
3336
rootdir.mkdirs()
3437
}
3538

36-
println("-- Gist plug-in initialized --")
3739
}
3840

3941
override val repositoryRoutings = Seq(

src/main/scala/gitbucket/gist/controller/GistController.scala

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ trait GistControllerBase extends ControllerBase {
9595
redirect(s"/gist")
9696

9797
} else {
98-
val isPrivate = params("private").toBoolean
99-
val description = params("description")
98+
val mode = Mode.from(params("mode"))
99+
val description = params("description")
100100

101101
// Create new repository
102102
val repoName = StringUtil.md5(loginAccount.userName + " " + datetime(new java.util.Date()))
@@ -108,9 +108,9 @@ trait GistControllerBase extends ControllerBase {
108108
registerGist(
109109
loginAccount.userName,
110110
repoName,
111-
isPrivate,
112111
getTitle(files.head._1, repoName),
113-
description
112+
description,
113+
mode
114114
)
115115

116116
// Commit files
@@ -130,13 +130,15 @@ trait GistControllerBase extends ControllerBase {
130130
val loginAccount = context.loginAccount.get
131131
val files = getFileParameters(true)
132132
val description = params("description")
133+
val mode = Mode.from(params("mode"))
133134

134135
// Update database
135136
updateGist(
136137
userName,
137138
repoName,
138139
getTitle(files.head._1, repoName),
139-
description
140+
description,
141+
mode
140142
)
141143

142144
// Commit files
@@ -170,28 +172,6 @@ trait GistControllerBase extends ControllerBase {
170172
}
171173
})
172174

173-
get("/gist/:userName/:repoName/secret")(editorOnly {
174-
val userName = params("userName")
175-
val repoName = params("repoName")
176-
177-
if(isEditable(userName)){
178-
updateGistAccessibility(userName, repoName, true)
179-
}
180-
181-
redirect(s"/gist/${userName}/${repoName}")
182-
})
183-
184-
get("/gist/:userName/:repoName/public")(editorOnly {
185-
val userName = params("userName")
186-
val repoName = params("repoName")
187-
188-
if(isEditable(userName)){
189-
updateGistAccessibility(userName, repoName, false)
190-
}
191-
192-
redirect(s"/gist/${userName}/${repoName}")
193-
})
194-
195175
get("/gist/:userName/:repoName/revisions"){
196176
val userName = params("userName")
197177
val repoName = params("repoName")
@@ -235,7 +215,7 @@ trait GistControllerBase extends ControllerBase {
235215
using(Git.open(gitdir)){ git =>
236216
val gist = getGist(userName, repoName).get
237217

238-
if(!gist.isPrivate || context.loginAccount.exists(x => x.isAdmin || x.userName == userName)){
218+
if(gist.mode == "PUBLIC" || context.loginAccount.exists(x => x.isAdmin || x.userName == userName)){
239219
JGitUtil.getFileList(git, revision, ".").find(_.name == fileName).map { file =>
240220
defining(JGitUtil.getContentFromId(git, file.id, false).get){ bytes =>
241221
RawData(FileUtil.getContentType(file.name, bytes), bytes)
@@ -326,7 +306,7 @@ trait GistControllerBase extends ControllerBase {
326306
val originUserName = gist.originUserName.getOrElse(gist.userName)
327307
val originRepoName = gist.originRepositoryName.getOrElse(gist.repositoryName)
328308

329-
registerGist(loginAccount.userName, repoName, gist.isPrivate, gist.title, gist.description,
309+
registerGist(loginAccount.userName, repoName, gist.title, gist.description, Mode.from(gist.mode),
330310
Some(originUserName), Some(originRepoName))
331311

332312
// Clone repository
@@ -442,7 +422,7 @@ trait GistControllerBase extends ControllerBase {
442422
////////////////////////////////////////////////////////////////////////////////
443423

444424

445-
private def _gist(userName: String, repoName: Option[String] = None, revision: String = "master"): Html = {
425+
private def _gist(userName: String, repoName: Option[String] = None, revision: String = "master"): Any = {
446426
repoName match {
447427
case None => {
448428
val page = params.get("page") match {
@@ -467,22 +447,33 @@ trait GistControllerBase extends ControllerBase {
467447
}
468448
case Some(repoName) => {
469449
val gist = getGist(userName, repoName).get
470-
val originUserName = gist.originUserName.getOrElse(userName)
471-
val originRepoName = gist.originRepositoryName.getOrElse(repoName)
472-
473-
html.detail(
474-
gist,
475-
getForkedCount(originUserName, originRepoName),
476-
GistRepositoryURL(gist, baseUrl, context.settings),
477-
revision,
478-
getGistFiles(userName, repoName, revision),
479-
getGistComments(userName, repoName),
480-
isEditable(userName)
481-
)
450+
if(gist.mode == "PRIVATE"){
451+
context.loginAccount match {
452+
case Some(x) if(x.userName == userName) => _gistDetail(gist, userName, repoName, revision)
453+
case _ => Unauthorized()
454+
}
455+
} else {
456+
_gistDetail(gist, userName, repoName, revision)
457+
}
482458
}
483459
}
484460
}
485461

462+
private def _gistDetail(gist: Gist, userName: String, repoName: String, revision: String): Html = {
463+
val originUserName = gist.originUserName.getOrElse(userName)
464+
val originRepoName = gist.originRepositoryName.getOrElse(repoName)
465+
466+
html.detail(
467+
gist,
468+
getForkedCount(originUserName, originRepoName),
469+
GistRepositoryURL(gist, baseUrl, context.settings),
470+
revision,
471+
getGistFiles(userName, repoName, revision),
472+
getGistComments(userName, repoName),
473+
isEditable(userName)
474+
)
475+
}
476+
486477
private def getGistFiles(userName: String, repoName: String, revision: String = "master"): Seq[(String, String)] = {
487478
val gitdir = new File(GistRepoDir, userName + "/" + repoName)
488479
using(Git.open(gitdir)){ git =>

src/main/scala/gitbucket/gist/model/Gist.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ trait GistComponent { self: gitbucket.core.model.Profile =>
99
class Gists(tag: Tag) extends Table[Gist](tag, "GIST") {
1010
val userName = column[String]("USER_NAME")
1111
val repositoryName = column[String]("REPOSITORY_NAME")
12-
val isPrivate = column[Boolean]("PRIVATE")
1312
val title = column[String]("TITLE")
1413
val description = column[String]("DESCRIPTION")
1514
val registeredDate = column[java.util.Date]("REGISTERED_DATE")
1615
val updatedDate = column[java.util.Date]("UPDATED_DATE")
1716
val originUserName = column[String]("ORIGIN_USER_NAME")
1817
val originRepositoryName = column[String]("ORIGIN_REPOSITORY_NAME")
19-
def * = (userName, repositoryName, isPrivate, title, description, registeredDate, updatedDate, originUserName.?, originRepositoryName.?) <> (Gist.tupled, Gist.unapply)
18+
val mode = column[String]("MODE")
19+
def * = (userName, repositoryName, title, description, registeredDate, updatedDate, originUserName.?, originRepositoryName.?, mode) <> (Gist.tupled, Gist.unapply)
2020
}
2121
}
2222

2323
case class Gist(
2424
userName: String,
2525
repositoryName: String,
26-
isPrivate: Boolean,
2726
title: String,
2827
description: String,
2928
registeredDate: java.util.Date,
3029
updatedDate: java.util.Date,
3130
originUserName: Option[String],
32-
originRepositoryName: Option[String]
31+
originRepositoryName: Option[String],
32+
mode: String
3333
){
3434
def toRepositoryInfo = {
3535
gitbucket.core.service.RepositoryService.RepositoryInfo(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package gitbucket.gist.model
2+
3+
sealed trait Mode {
4+
val code: String
5+
}
6+
7+
object Mode {
8+
9+
def from(code: String): Mode = {
10+
code match {
11+
case Public.code => Public
12+
case Secret.code => Public
13+
case Private.code => Public
14+
}
15+
}
16+
17+
case object Public extends Mode {
18+
val code = "PUBLIC"
19+
}
20+
21+
case object Secret extends Mode {
22+
val code = "SECRET"
23+
}
24+
25+
case object Private extends Mode {
26+
val code = "PRIVATE"
27+
}
28+
29+
}

src/main/scala/gitbucket/gist/service/GistService.scala

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitbucket.gist.service
22

33
import gitbucket.core.model.Account
44
import gitbucket.gist.model.Gist
5+
import gitbucket.gist.model.Mode
56
import gitbucket.gist.model.Profile._
67
import profile.simple._
78

@@ -12,29 +13,29 @@ trait GistService {
1213

1314
def getVisibleGists(offset: Int, limit: Int, account: Option[Account])(implicit s: Session): Seq[Gist] = {
1415
val query = account.map { x =>
15-
Gists.filter { t => (t.isPrivate === false.bind) || (t.userName === x.userName.bind) }
16+
Gists.filter { t => (t.mode === "PUBLIC".bind) || (t.userName === x.userName.bind) }
1617
} getOrElse {
17-
Gists.filter { t => (t.isPrivate === false.bind) }
18+
Gists.filter { t => (t.mode === "PUBLIC".bind) }
1819
}
1920
query.sortBy(_.registeredDate desc).drop(offset).take(limit).list
2021
}
2122

2223
def countPublicGists()(implicit s: Session): Int =
23-
Query(Gists.filter(_.isPrivate === false.bind).length).first
24+
Query(Gists.filter(_.mode === "PUBLIC".bind).length).first
2425

2526
def getUserGists(userName: String, loginUserName: Option[String], offset: Int, limit: Int)(implicit s: Session): Seq[Gist] =
2627
(if(loginUserName.isDefined){
27-
Gists filter(t => (t.userName === userName.bind) && ((t.userName === loginUserName.bind) || (t.isPrivate === false.bind)))
28+
Gists filter(t => (t.userName === userName.bind) && ((t.userName === loginUserName.bind) || (t.mode === "PUBLIC".bind)))
2829
} else {
29-
Gists filter(t => (t.userName === userName.bind) && (t.isPrivate === false.bind))
30+
Gists filter(t => (t.userName === userName.bind) && (t.mode === "PUBLIC".bind))
3031
}).sortBy(_.registeredDate desc).drop(offset).take(limit).list
3132

3233

3334
def countUserGists(userName: String, loginUserName: Option[String])(implicit s: Session): Int =
3435
Query((if(loginUserName.isDefined){
35-
Gists.filter(t => (t.userName === userName.bind) && ((t.userName === loginUserName.bind) || (t.isPrivate === false.bind)))
36+
Gists.filter(t => (t.userName === userName.bind) && ((t.userName === loginUserName.bind) || (t.mode === "PUBLIC".bind)))
3637
} else {
37-
Gists.filter(t => (t.userName === userName.bind) && (t.isPrivate === false.bind))
38+
Gists.filter(t => (t.userName === userName.bind) && (t.mode === "PUBLIC".bind))
3839
}).length).first
3940

4041
def getGist(userName: String, repositoryName: String)(implicit s: Session): Option[Gist] =
@@ -46,23 +47,16 @@ trait GistService {
4647
def getForkedGists(userName: String, repositoryName: String)(implicit s: Session): Seq[Gist] =
4748
Gists.filter(t => (t.originUserName === userName.bind) && (t.originRepositoryName === repositoryName.bind)).sortBy(_.userName).list
4849

49-
def registerGist(userName: String, repositoryName: String, isPrivate: Boolean, title: String, description: String,
50+
def registerGist(userName: String, repositoryName: String, title: String, description: String, mode: Mode,
5051
originUserName: Option[String] = None, originRepositoryName: Option[String] = None)(implicit s: Session): Unit =
51-
Gists.insert(Gist(userName, repositoryName, isPrivate, title, description, new java.util.Date(), new java.util.Date(),
52-
originUserName, originRepositoryName))
52+
Gists.insert(Gist(userName, repositoryName, title, description, new java.util.Date(), new java.util.Date(),
53+
originUserName, originRepositoryName, mode.code))
5354

54-
def updateGist(userName: String, repositoryName: String, title: String, description: String)(implicit s: Session): Unit =
55+
def updateGist(userName: String, repositoryName: String, title: String, description: String, mode: Mode)(implicit s: Session): Unit =
5556
Gists
5657
.filter(t => (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind))
57-
.map(t => (t.title, t.description, t.updatedDate))
58-
.update(title, description, new java.util.Date())
59-
60-
def updateGistAccessibility(userName: String, repositoryName: String, isPrivate: Boolean)(implicit s: Session): Unit =
61-
Gists
62-
.filter(t => (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind))
63-
.map(t => (t.isPrivate))
64-
.update(isPrivate)
65-
58+
.map(t => (t.title, t.description, t.updatedDate, t.mode))
59+
.update(title, description, new java.util.Date(), mode.code)
6660

6761
def deleteGist(userName: String, repositoryName: String)(implicit s: Session): Unit = {
6862
GistComments.filter(t => (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind)).delete

src/main/twirl/gitbucket/gist/detail.scala.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@gitbucket.gist.html.header(gist, forkedCount, editable)
1313
<div class="container body">
1414
@gitbucket.gist.html.menu("code", gist, repositoryUrl)
15-
<div style="margin-right: 260px;">
15+
<div style="margin-right: 260px; overflow: hidden;">
1616
<div style="margin-bottom: 10px;">
1717
@gist.description
1818
</div>

src/main/twirl/gitbucket/gist/edit.scala.html

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@(gist: Option[gitbucket.gist.model.Gist],
22
files: Seq[(String, gitbucket.core.util.JGitUtil.ContentInfo)])(implicit context: gitbucket.core.controller.Context)
33
@import context._
4+
@import gitbucket.gist.model.Mode
45
@import gitbucket.core.view.helpers._
56
@gitbucket.core.html.main("Snippets"){
67
@gitbucket.gist.html.style()
@@ -13,15 +14,20 @@ <h1 style="margin: 0px;">New snippet</h1>
1314
@avatar(gist.get.userName, 24)
1415
Editing
1516
<a class="strong" href="@path/gist/@gist.get.userName/@gist.get.repositoryName">@gist.get.title</a>
16-
@if(gist.get.isPrivate){
17+
@if(gist.get.mode == Mode.Secret.code){
1718
<span class="label label-warning">Secret</span>
1819
}
20+
@if(gist.get.mode == Mode.Private.code){
21+
<span class="label label-warning">Private</span>
22+
}
1923
<div class="pull-right">
20-
@if(gist.get.isPrivate){
24+
@*
25+
@if(gist.get.mode == "SECRET"){
2126
<a href="@path/gist/@gist.get.userName/@gist.get.repositoryName/public" class="btn btn-default">Make public</a>
2227
} else {
2328
<a href="@path/gist/@gist.get.userName/@gist.get.repositoryName/secret" class="btn btn-default">Make secret</a>
2429
}
30+
*@
2531
<a href="@path/gist/@gist.get.userName/@gist.get.repositoryName/delete" class="btn btn-danger" id="delete">Delete</a>
2632
</div>
2733
<div class="muted" style="margin-left: 30px; font-size: 80%;">
@@ -44,10 +50,16 @@ <h1 style="margin: 0px;">New snippet</h1>
4450
<div class="pull-right">
4551
@if(gist.isDefined){
4652
<a href="@path/gist/@gist.get.userName/@gist.get.repositoryName" class="btn btn-default">Cancel</a>
53+
}
54+
<div class="btn-group" data-toggle="buttons">
55+
<label class="btn btn-default btn-mini @if(gist.isEmpty || gist.get.mode == Mode.Public.code ){active}"><input type="radio" value="PUBLIC" name="mode">Public</label>
56+
<label class="btn btn-default btn-mini @if(gist.isDefined && gist.get.mode == Mode.Secret.code ){active}"><input type="radio" value="SECRET" name="mode">Secret</label>
57+
<label class="btn btn-default btn-mini @if(gist.isDefined && gist.get.mode == Mode.Private.code){active}"><input type="radio" value="PRIVATE" name="mode">Private</label>
58+
</div>
59+
@if(gist.isDefined){
4760
<input type="submit" value="Update" class="btn btn-success submit_snippet" id="update_snippet">
4861
} else {
49-
<input type="submit" value="Create private Snippet" class="btn btn-default submit_snippet" id="create_private_snippet">
50-
<input type="submit" value="Create public Snippet" class="btn btn-default submit_snippet" id="create_public_snippet">
62+
<input type="submit" value="Create" class="btn btn-success submit_snippet" id="create_snippet">
5163
}
5264
</div>
5365
</div>
@@ -64,14 +76,8 @@ <h1 style="margin: 0px;">New snippet</h1>
6476
<link href="@{gitbucket.core.view.helpers.assets}/vendors/jsdifflib/diffview.css" type="text/css" rel="stylesheet" />
6577
<script>
6678
$(function(){
67-
$('#create_private_snippet').click(function(){
68-
$('#form').attr('action', '@path/gist/_new')
69-
$('#private').val('true');
70-
});
71-
72-
$('#create_public_snippet').click(function(){
79+
$('#create_snippet').click(function(){
7380
$('#form').attr('action', '@path/gist/_new')
74-
$('#private').val('false');
7581
});
7682

7783
$('#add_file').click(function(){

0 commit comments

Comments
 (0)