aboutsummaryrefslogtreecommitdiff
path: root/src/controller.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/controller.rs')
-rw-r--r--src/controller.rs78
1 files changed, 46 insertions, 32 deletions
diff --git a/src/controller.rs b/src/controller.rs
index 2d8b73f..f912d74 100644
--- a/src/controller.rs
+++ b/src/controller.rs
@@ -36,7 +36,7 @@ pub trait Controller {
}
/// Returns an HTML string describing who has access to the context.
- fn access_info_html(&self, ctx: &Context) -> Option<String> {
+ fn access_info_html(&self, ctx: &Context, parts: &Parts) -> Option<String> {
None
}
@@ -44,27 +44,33 @@ pub trait Controller {
fn signature(&self, repo: &Repository, parts: &Parts) -> Result<Signature, Error>;
/// Returns whether or not a request is authorized to read a file or list a directory.
- fn may_read_path(&self, context: &Context) -> bool;
+ fn may_read_path(&self, ctx: &Context, parts: &Parts) -> bool;
/// Returns whether or not a request is authorized to write a file.
- fn may_write_path(&self, context: &Context) -> bool;
+ fn may_write_path(&self, context: &Context, parts: &Parts) -> bool;
/// Returns whether or not a request is authorized to (re)move a file.
- fn may_move_path(&self, context: &Context) -> bool;
+ fn may_move_path(&self, context: &Context, parts: &Parts) -> bool;
fn edit_hint_html(&self, context: &Context) -> Option<String> {
None
}
- fn before_return_tree_page(&self, page: &mut Page, tree: Option<Tree>, context: &Context);
+ fn before_return_tree_page(
+ &self,
+ page: &mut Page,
+ tree: Option<Tree>,
+ context: &Context,
+ parts: &Parts,
+ );
/// Executed before writing a file. Return an error to abort the writing process.
- fn before_write(&self, text: &str, context: &mut Context) -> Result<(), String> {
+ fn before_write(&self, text: &str, ctx: &Context, parts: &mut Parts) -> Result<(), String> {
Ok(())
}
/// Executed after successfully writing a file.
- fn after_write(&self, context: &mut Context) {}
+ fn after_write(&self, context: &Context, parts: &mut Parts) {}
}
pub struct SoloController(pub Branch);
@@ -98,25 +104,31 @@ impl Controller for SoloController {
repo.signature().map_err(|e| Error::Internal(e.to_string()))
}
- fn may_read_path(&self, _context: &Context) -> bool {
+ fn may_read_path(&self, ctx: &Context, parts: &Parts) -> bool {
true
}
- fn may_write_path(&self, _context: &Context) -> bool {
+ fn may_write_path(&self, _context: &Context, parts: &Parts) -> bool {
true
}
- fn may_move_path(&self, _context: &Context) -> bool {
+ fn may_move_path(&self, _context: &Context, parts: &Parts) -> bool {
true
}
- fn before_return_tree_page(&self, page: &mut Page, tree: Option<Tree>, context: &Context) {
+ fn before_return_tree_page(
+ &self,
+ page: &mut Page,
+ tree: Option<Tree>,
+ context: &Context,
+ parts: &Parts,
+ ) {
if tree.map(|t| t.len()).unwrap_or_default() == 0 {
page.body.push_str("<p>create files by editing the URL, e.g. <a href='/hello-world.md'>/hello-world.md</a></p>");
}
}
- fn before_write(&self, text: &str, ctx: &mut Context) -> Result<(), String> {
+ fn before_write(&self, text: &str, ctx: &Context, parts: &mut Parts) -> Result<(), String> {
if let Some(ext) = ctx.path.extension().and_then(|e| e.to_str()) {
validate_formats(text, ext)?;
}
@@ -274,8 +286,6 @@ fn multi_user_startpage(
// TODO: add domain name to title?
let mut page = Page {
title: "GitPad".into(),
- controller,
- parts: &parts,
body: String::new(),
header: None,
};
@@ -348,8 +358,6 @@ impl Controller for MultiUserController {
title: "".into(),
header: None,
body: String::new(),
- controller: self,
- parts,
};
self.list_shares(repo, &rev, username, &mut page.body);
@@ -371,8 +379,14 @@ impl Controller for MultiUserController {
))
}
- fn before_return_tree_page(&self, page: &mut Page, tree: Option<Tree>, context: &Context) {
- let username = username_from_parts(&context.parts).unwrap();
+ fn before_return_tree_page(
+ &self,
+ page: &mut Page,
+ tree: Option<Tree>,
+ context: &Context,
+ parts: &Parts,
+ ) {
+ let username = username_from_parts(&parts).unwrap();
if context.path.components().count() == 0 {
if context.branch.0 == username {
match tree {
@@ -405,8 +419,8 @@ impl Controller for MultiUserController {
}
}
- fn may_read_path(&self, ctx: &Context) -> bool {
- let username = username_from_parts(&ctx.parts).unwrap();
+ fn may_read_path(&self, ctx: &Context, parts: &Parts) -> bool {
+ let username = username_from_parts(parts).unwrap();
if ctx.branch.0 == username {
return true;
}
@@ -420,8 +434,8 @@ impl Controller for MultiUserController {
ok
}
- fn may_write_path(&self, ctx: &Context) -> bool {
- let username = username_from_parts(&ctx.parts).unwrap();
+ fn may_write_path(&self, ctx: &Context, parts: &Parts) -> bool {
+ let username = username_from_parts(&parts).unwrap();
if ctx.branch.0 == username {
return true;
}
@@ -437,8 +451,8 @@ impl Controller for MultiUserController {
ok
}
- fn may_move_path(&self, ctx: &Context) -> bool {
- ctx.branch.0 == username_from_parts(&ctx.parts).unwrap()
+ fn may_move_path(&self, ctx: &Context, parts: &Parts) -> bool {
+ ctx.branch.0 == username_from_parts(&parts).unwrap()
}
fn edit_hint_html(&self, ctx: &Context) -> Option<String> {
@@ -454,13 +468,13 @@ impl Controller for MultiUserController {
None
}
- fn before_write(&self, text: &str, ctx: &mut Context) -> Result<(), String> {
+ fn before_write(&self, text: &str, ctx: &Context, parts: &mut Parts) -> Result<(), String> {
match (ctx.branch.0.as_str(), ctx.path.to_str().unwrap()) {
(_, ".shares.txt") => {
- ctx.parts.extensions.insert(parse_shares_txt(text)?);
+ parts.extensions.insert(parse_shares_txt(text)?);
}
("gitpad", "users.toml") => {
- ctx.parts
+ parts
.extensions
.insert(toml::from_str::<Identities>(text).map_err(|e| e.to_string())?);
}
@@ -473,23 +487,23 @@ impl Controller for MultiUserController {
Ok(())
}
- fn after_write(&self, ctx: &mut Context) {
+ fn after_write(&self, ctx: &Context, parts: &mut Parts) {
match (ctx.branch.0.as_str(), ctx.path.to_str().unwrap()) {
(_, ".shares.txt") => {
self.shares_cache
.write()
.unwrap()
- .insert(ctx.branch.clone(), ctx.parts.extensions.remove().unwrap());
+ .insert(ctx.branch.clone(), parts.extensions.remove().unwrap());
}
("gitpad", "users.toml") => {
- *self.identities.write().unwrap() = ctx.parts.extensions.remove().unwrap();
+ *self.identities.write().unwrap() = parts.extensions.remove().unwrap();
}
_ => {}
}
}
- fn access_info_html(&self, ctx: &Context) -> Option<String> {
- let own_username = username_from_parts(&ctx.parts).unwrap();
+ fn access_info_html(&self, ctx: &Context, parts: &Parts) -> Option<String> {
+ let own_username = username_from_parts(&parts).unwrap();
if own_username != ctx.branch.0 {
return None;
}