Skip to content

Commit 181e521

Browse files
committed
Group support
1 parent 7bebf1f commit 181e521

File tree

6 files changed

+51
-21
lines changed

6 files changed

+51
-21
lines changed

src/main/scala/Plugin.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Plugin extends gitbucket.core.plugin.Plugin {
6464
(context: Context) => Some(Link("snippets", "Snippets", "gist"))
6565
)
6666
override val profileTabs = Seq(
67-
(account: Account, context: Context) => if(account.isGroupAccount) None else Some(Link("snippets", "Snippets", s"gist/${account.userName}/_profile"))
67+
(account: Account, context: Context) => Some(Link("snippets", "Snippets", s"gist/${account.userName}/_profile"))
6868
)
6969
override val assetsMappings = Seq("/gist" -> "/gitbucket/gist/assets")
7070

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,18 @@ trait GistControllerBase extends ControllerBase {
9292
val files: Seq[(String, JGitUtil.ContentInfo)] = JGitUtil.getFileList(git, "master", ".").map { file =>
9393
(if(isGistFile(file.name)) "" else file.name) -> JGitUtil.getContentInfo(git, file.name, file.id)
9494
}
95-
html.edit(getGist(userName, repoName), files)
95+
html.edit(getGist(userName, repoName), files, None)
9696
}
9797
}
9898
})
9999

100100
post("/gist/_new")(usersOnly {
101101
if(context.loginAccount.isDefined){
102102
val loginAccount = context.loginAccount.get
103+
val userName = params.getOrElse("userName", loginAccount.userName)
104+
105+
// TODO check
106+
103107
val files = getFileParameters()
104108

105109
if(files.isEmpty){
@@ -110,14 +114,14 @@ trait GistControllerBase extends ControllerBase {
110114
val description = params("description")
111115

112116
// Create new repository
113-
val repoName = StringUtil.md5(loginAccount.userName + " " + datetime(new java.util.Date()))
114-
val gitdir = new File(GistRepoDir, loginAccount.userName + "/" + repoName)
117+
val repoName = StringUtil.md5(userName + " " + datetime(new java.util.Date()))
118+
val gitdir = new File(GistRepoDir, userName + "/" + repoName)
115119
gitdir.mkdirs()
116120
JGitUtil.initRepository(gitdir)
117121

118122
// Insert record
119123
registerGist(
120-
loginAccount.userName,
124+
userName,
121125
repoName,
122126
getTitle(files.head._1, repoName),
123127
description,
@@ -129,7 +133,7 @@ trait GistControllerBase extends ControllerBase {
129133
commitFiles(git, loginAccount, "Initial commit", files)
130134
}
131135

132-
redirect(s"/gist/${loginAccount.userName}/${repoName}")
136+
redirect(s"/gist/${userName}/${repoName}")
133137
}
134138
}
135139
})
@@ -173,7 +177,7 @@ trait GistControllerBase extends ControllerBase {
173177
val userName = params("userName")
174178
val repoName = params("repoName")
175179

176-
if(isEditable(userName)){
180+
if(isEditable(userName, loginUserGroups)){
177181
deleteGist(userName, repoName)
178182

179183
val gitdir = new File(GistRepoDir, userName + "/" + repoName)
@@ -205,7 +209,7 @@ trait GistControllerBase extends ControllerBase {
205209
gist,
206210
getForkedCount(originUserName, originRepoName),
207211
GistRepositoryURL(gist, baseUrl, context.settings),
208-
isEditable(userName),
212+
isEditable(userName, loginUserGroups),
209213
commits
210214
)
211215
}
@@ -268,12 +272,18 @@ trait GistControllerBase extends ControllerBase {
268272
getUserGists(userName, context.loginAccount.map(_.userName), 0, Limit),
269273
countUserGists(userName, context.loginAccount.map(_.userName))
270274
)
275+
276+
val createSnippet = context.loginAccount.exists { loginAccount =>
277+
loginAccount.userName == userName || getGroupsByUserName(loginAccount.userName).contains(userName)
278+
}
279+
271280
getAccountByUserName(userName).map { account =>
272281
html.profile(
273-
account,
274-
if(account.isGroupAccount) Nil else getGroupsByUserName(userName),
275-
getAccountExtraMailAddresses(userName),
276-
result._1
282+
account = account,
283+
groupNames = if(account.isGroupAccount) Nil else getGroupsByUserName(userName),
284+
extraMailAddresses = getAccountExtraMailAddresses(userName),
285+
gists = result._1,
286+
createSnippet = createSnippet
277287
)
278288
} getOrElse NotFound
279289
}
@@ -283,7 +293,11 @@ trait GistControllerBase extends ControllerBase {
283293
}
284294

285295
get("/gist/_new")(usersOnly {
286-
html.edit(None, Seq(("", JGitUtil.ContentInfo("text", None, None, Some("UTF-8")))))
296+
val userName = params.get("userName")
297+
298+
if(isEditable(userName.getOrElse(context.loginAccount.get.userName), loginUserGroups)){
299+
html.edit(None, Seq(("", JGitUtil.ContentInfo("text", None, None, Some("UTF-8")))), userName)
300+
} else Unauthorized()
287301
})
288302

289303
get("/gist/_add"){
@@ -335,7 +349,7 @@ trait GistControllerBase extends ControllerBase {
335349
getForkedCount(userName, repoName),
336350
GistRepositoryURL(gist, baseUrl, context.settings),
337351
getForkedGists(userName, repoName),
338-
isEditable(userName)
352+
isEditable(userName, loginUserGroups)
339353
)
340354
} getOrElse NotFound
341355
}
@@ -503,7 +517,7 @@ trait GistControllerBase extends ControllerBase {
503517
revision,
504518
getGistFiles(userName, repoName, revision),
505519
getGistComments(userName, repoName),
506-
isEditable(userName)
520+
isEditable(userName, loginUserGroups)
507521
)
508522
}
509523

@@ -526,4 +540,10 @@ trait GistControllerBase extends ControllerBase {
526540
}
527541
}
528542

543+
private def loginUserGroups: Seq[String] = {
544+
context.loginAccount.map { account =>
545+
getGroupsByUserName(account.userName)
546+
}.getOrElse(Nil)
547+
}
548+
529549
}

src/main/scala/gitbucket/gist/util/GistAuthenticator.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package gitbucket.gist.util
22

33
import gitbucket.core.controller.ControllerBase
4+
import gitbucket.core.service.AccountService
45
import gitbucket.core.util.SyntaxSugars._
56
import gitbucket.core.util.Implicits._
67

78
/**
89
* Allows only editor of the accessed snippet.
910
*/
10-
trait GistEditorAuthenticator { self: ControllerBase =>
11+
trait GistEditorAuthenticator { self: ControllerBase with AccountService =>
1112
protected def editorOnly(action: => Any) = { authenticate(action) }
1213
protected def editorOnly[T](action: T => Any) = (form: T) => { authenticate(action(form)) }
1314

1415
private def authenticate(action: => Any) = {
1516
{
1617
defining(request.paths){ paths =>
1718
if(context.loginAccount.map { loginAccount =>
18-
loginAccount.isAdmin || loginAccount.userName == paths(1)
19+
loginAccount.isAdmin || loginAccount.userName == paths(1) || getGroupsByUserName(loginAccount.userName).contains(paths(1))
1920
}.getOrElse(false)){
2021
action
2122
} else {

src/main/scala/gitbucket/gist/util/GistUtils.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import org.eclipse.jgit.lib.{FileMode, Constants, ObjectId}
1212

1313
object GistUtils {
1414

15-
def isEditable(userName: String)(implicit context: Context): Boolean = {
15+
def isEditable(userName: String, groupNames: Seq[String])(implicit context: Context): Boolean = {
1616
context.loginAccount.map { loginAccount =>
17-
loginAccount.isAdmin || loginAccount.userName == userName
17+
loginAccount.isAdmin || loginAccount.userName == userName || groupNames.contains(userName)
1818
}.getOrElse(false)
1919
}
2020

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@(gist: Option[gitbucket.gist.model.Gist],
2-
files: Seq[(String, gitbucket.core.util.JGitUtil.ContentInfo)])(implicit context: gitbucket.core.controller.Context)
2+
files: Seq[(String, gitbucket.core.util.JGitUtil.ContentInfo)],
3+
userName: Option[String])(implicit context: gitbucket.core.controller.Context)
34
@import gitbucket.gist.model.Mode
45
@import gitbucket.core.view.helpers
56
@gitbucket.core.html.main("Snippets"){
@@ -62,6 +63,9 @@ <h1 style="margin: 0px;">New snippet</h1>
6263
}
6364
</div>
6465
</div>
66+
@userName.map { userName =>
67+
<input type="hidden" id="userName" name="userName" value="@userName"/>
68+
}
6569
<input type="hidden" id="count" name="count" value="@files.size"/>
6670
</form>
6771
</div>

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
@(account: gitbucket.core.model.Account, groupNames: List[String], extraMailAddresses: List[String],
2-
gists: Seq[gitbucket.gist.model.Gist])(implicit context: gitbucket.core.controller.Context)
2+
gists: Seq[gitbucket.gist.model.Gist], createSnippet: Boolean)(implicit context: gitbucket.core.controller.Context)
33
@import gitbucket.gist.model.Mode
44
@gitbucket.core.account.html.main(account, groupNames, "snippets", extraMailAddresses){
5+
@if(createSnippet){
6+
<div class="pull-right">
7+
<a href="@context.path/gist/_new?userName=@account.userName" class="btn btn-success">Create snippet</a>
8+
</div>
9+
}
510
@if(gists.isEmpty){
611
No snippets
712
} else {

0 commit comments

Comments
 (0)