Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
takezoe committed Dec 11, 2018
2 parents 670446c + f60f8bb commit dbd8416
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Plugin extends gitbucket.core.plugin.Plugin {
(context: Context) => Some(Link("snippets", "Snippets", "gist"))
)
override val profileTabs = Seq(
(account: Account, context: Context) => if(account.isGroupAccount) None else Some(Link("snippets", "Snippets", s"gist/${account.userName}/_profile"))
(account: Account, context: Context) => Some(Link("snippets", "Snippets", s"gist/${account.userName}/_profile"))
)
override val assetsMappings = Seq("/gist" -> "/gitbucket/gist/assets")

Expand Down
55 changes: 36 additions & 19 deletions src/main/scala/gitbucket/gist/controller/GistController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,17 @@ trait GistControllerBase extends ControllerBase {
val files: Seq[(String, JGitUtil.ContentInfo)] = JGitUtil.getFileList(git, "master", ".").map { file =>
(if(isGistFile(file.name)) "" else file.name) -> JGitUtil.getContentInfo(git, file.name, file.id)
}
html.edit(getGist(userName, repoName), files)
html.edit(getGist(userName, repoName), files, None)
}
}
})

post("/gist/_new")(usersOnly {
if(context.loginAccount.isDefined){
val loginAccount = context.loginAccount.get
val files = getFileParameters()
val loginAccount = context.loginAccount.get
val userName = params.getOrElse("userName", loginAccount.userName)

if(isEditable(userName, loginUserGroups)) {
val files = getFileParameters()
if(files.isEmpty){
redirect(s"/gist")

Expand All @@ -110,14 +111,14 @@ trait GistControllerBase extends ControllerBase {
val description = params("description")

// Create new repository
val repoName = StringUtil.md5(loginAccount.userName + " " + datetime(new java.util.Date()))
val gitdir = new File(GistRepoDir, loginAccount.userName + "/" + repoName)
val repoName = StringUtil.md5(userName + " " + datetime(new java.util.Date()))
val gitdir = new File(GistRepoDir, userName + "/" + repoName)
gitdir.mkdirs()
JGitUtil.initRepository(gitdir)

// Insert record
registerGist(
loginAccount.userName,
userName,
repoName,
getTitle(files.head._1, repoName),
description,
Expand All @@ -129,9 +130,9 @@ trait GistControllerBase extends ControllerBase {
commitFiles(git, loginAccount, "Initial commit", files)
}

redirect(s"/gist/${loginAccount.userName}/${repoName}")
redirect(s"/gist/${userName}/${repoName}")
}
}
} else Unauthorized()
})

post("/gist/:userName/:repoName/edit")(editorOnly {
Expand Down Expand Up @@ -166,14 +167,14 @@ trait GistControllerBase extends ControllerBase {
refUpdate.update()
}

redirect(s"/gist/${loginAccount.userName}/${repoName}")
redirect(s"/gist/${userName}/${repoName}")
})

get("/gist/:userName/:repoName/delete")(editorOnly {
val userName = params("userName")
val repoName = params("repoName")

if(isEditable(userName)){
if(isEditable(userName, loginUserGroups)){
deleteGist(userName, repoName)

val gitdir = new File(GistRepoDir, userName + "/" + repoName)
Expand Down Expand Up @@ -205,7 +206,7 @@ trait GistControllerBase extends ControllerBase {
gist,
getForkedCount(originUserName, originRepoName),
GistRepositoryURL(gist, baseUrl, context.settings),
isEditable(userName),
isEditable(userName, loginUserGroups),
commits
)
}
Expand Down Expand Up @@ -268,12 +269,18 @@ trait GistControllerBase extends ControllerBase {
getUserGists(userName, context.loginAccount.map(_.userName), 0, Limit),
countUserGists(userName, context.loginAccount.map(_.userName))
)

val createSnippet = context.loginAccount.exists { loginAccount =>
loginAccount.userName == userName || getGroupsByUserName(loginAccount.userName).contains(userName)
}

getAccountByUserName(userName).map { account =>
html.profile(
account,
if(account.isGroupAccount) Nil else getGroupsByUserName(userName),
getAccountExtraMailAddresses(userName),
result._1
account = account,
groupNames = if(account.isGroupAccount) Nil else getGroupsByUserName(userName),
extraMailAddresses = getAccountExtraMailAddresses(userName),
gists = result._1,
createSnippet = createSnippet
)
} getOrElse NotFound
}
Expand All @@ -283,7 +290,11 @@ trait GistControllerBase extends ControllerBase {
}

get("/gist/_new")(usersOnly {
html.edit(None, Seq(("", JGitUtil.ContentInfo("text", None, None, Some("UTF-8")))))
val userName = params.get("userName")

if(isEditable(userName.getOrElse(context.loginAccount.get.userName), loginUserGroups)){
html.edit(None, Seq(("", JGitUtil.ContentInfo("text", None, None, Some("UTF-8")))), userName)
} else Unauthorized()
})

get("/gist/_add"){
Expand Down Expand Up @@ -335,7 +346,7 @@ trait GistControllerBase extends ControllerBase {
getForkedCount(userName, repoName),
GistRepositoryURL(gist, baseUrl, context.settings),
getForkedGists(userName, repoName),
isEditable(userName)
isEditable(userName, loginUserGroups)
)
} getOrElse NotFound
}
Expand Down Expand Up @@ -504,7 +515,7 @@ trait GistControllerBase extends ControllerBase {
revision,
getGistFiles(userName, repoName, revision),
getGistComments(userName, repoName),
isEditable(userName)
isEditable(userName, loginUserGroups)
)
}

Expand All @@ -527,4 +538,10 @@ trait GistControllerBase extends ControllerBase {
}
}

private def loginUserGroups: Seq[String] = {
context.loginAccount.map { account =>
getGroupsByUserName(account.userName)
}.getOrElse(Nil)
}

}
5 changes: 3 additions & 2 deletions src/main/scala/gitbucket/gist/util/GistAuthenticator.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package gitbucket.gist.util

import gitbucket.core.controller.ControllerBase
import gitbucket.core.service.AccountService
import gitbucket.core.util.SyntaxSugars._
import gitbucket.core.util.Implicits._

/**
* Allows only editor of the accessed snippet.
*/
trait GistEditorAuthenticator { self: ControllerBase =>
trait GistEditorAuthenticator { self: ControllerBase with AccountService =>
protected def editorOnly(action: => Any) = { authenticate(action) }
protected def editorOnly[T](action: T => Any) = (form: T) => { authenticate(action(form)) }

private def authenticate(action: => Any) = {
{
defining(request.paths){ paths =>
if(context.loginAccount.map { loginAccount =>
loginAccount.isAdmin || loginAccount.userName == paths(1)
loginAccount.isAdmin || loginAccount.userName == paths(1) || getGroupsByUserName(loginAccount.userName).contains(paths(1))
}.getOrElse(false)){
action
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/gitbucket/gist/util/GistUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import org.eclipse.jgit.lib.{FileMode, Constants, ObjectId}

object GistUtils {

def isEditable(userName: String)(implicit context: Context): Boolean = {
def isEditable(userName: String, groupNames: Seq[String])(implicit context: Context): Boolean = {
context.loginAccount.map { loginAccount =>
loginAccount.isAdmin || loginAccount.userName == userName
loginAccount.isAdmin || loginAccount.userName == userName || groupNames.contains(userName)
}.getOrElse(false)
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/twirl/gitbucket/gist/edit.scala.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@(gist: Option[gitbucket.gist.model.Gist],
files: Seq[(String, gitbucket.core.util.JGitUtil.ContentInfo)])(implicit context: gitbucket.core.controller.Context)
files: Seq[(String, gitbucket.core.util.JGitUtil.ContentInfo)],
userName: Option[String])(implicit context: gitbucket.core.controller.Context)
@import gitbucket.gist.model.Mode
@import gitbucket.core.view.helpers
@gitbucket.core.html.main("Snippets"){
Expand Down Expand Up @@ -62,6 +63,9 @@ <h1 style="margin: 0px;">New snippet</h1>
}
</div>
</div>
@userName.map { userName =>
<input type="hidden" id="userName" name="userName" value="@userName"/>
}
<input type="hidden" id="count" name="count" value="@files.size"/>
</form>
</div>
Expand Down
7 changes: 6 additions & 1 deletion src/main/twirl/gitbucket/gist/profile.scala.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
@(account: gitbucket.core.model.Account, groupNames: List[String], extraMailAddresses: List[String],
gists: Seq[gitbucket.gist.model.Gist])(implicit context: gitbucket.core.controller.Context)
gists: Seq[gitbucket.gist.model.Gist], createSnippet: Boolean)(implicit context: gitbucket.core.controller.Context)
@import gitbucket.gist.model.Mode
@gitbucket.core.account.html.main(account, groupNames, "snippets", extraMailAddresses){
@if(createSnippet){
<div class="pull-right">
<a href="@context.path/gist/[email protected]" class="btn btn-success">Create snippet</a>
</div>
}
@if(gists.isEmpty){
No snippets
} else {
Expand Down

0 comments on commit dbd8416

Please sign in to comment.