Skip to content

Commit

Permalink
Merge pull request #39 from robur-coop/superadmin
Browse files Browse the repository at this point in the history
Superadmins
  • Loading branch information
hannesm authored Sep 11, 2024
2 parents 54baf33 + eb4dac9 commit 35db1c6
Show file tree
Hide file tree
Showing 10 changed files with 587 additions and 660 deletions.
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ freebsd_instance:
freebsd_task:
pkg_install_script: pkg install -y ocaml-opam gmake bash
ocaml_script: opam init -a --comp=4.14.2
mirage_script: eval `opam env` && opam install --confirm-level=unsafe-yes "mirage>=4.5.0"
mirage_script: eval `opam env` && opam install --confirm-level=unsafe-yes "mirage<4.7.0"
configure_script: eval `opam env` && mirage configure -t hvt
depend_script: eval `opam env` && gmake depend
build_script: eval `opam env` && gmake build
Expand Down
40 changes: 25 additions & 15 deletions assets/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,30 @@ function buttonLoading(btn, load, text) {
}
}

async function toggleUserStatus(uuid) {
try {
const response = await fetch("/api/admin/user/status/toggle", {
method: 'POST',
body: JSON.stringify({"uuid":uuid})
})
const data = await response.json();
if(data.status === 200) {
async function toggleUserStatus(uuid, endpoint) {
try {
const response = await fetch(endpoint, {
method: 'POST',
body: JSON.stringify({ uuid: uuid }),
headers: { 'Content-Type': 'application/json' }
});

const data = await response.json();
if (response.status === 200) {
postAlert("bg-primary-300", data.data);
setTimeout(function () { window.location.reload()}, 1000);
} else {
setTimeout(() => window.location.reload(), 1000);
} else {
postAlert("bg-secondary-300", data.data);
}
} catch (error) {
postAlert("bg-secondary-300", error);
}
}
}
} catch (error) {
postAlert("bg-secondary-300", error);
}
}

async function toggleUserActiveStatus(uuid) {
await toggleUserStatus(uuid, "/api/admin/user/activate/toggle");
}

async function toggleUserAdminStatus(uuid) {
await toggleUserStatus(uuid, "/api/admin/user/admin/toggle");
}
2 changes: 1 addition & 1 deletion assets/style.css

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion config.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(* mirage >= 4.6.0 & < 4.7.0 *)
open Mirage

let assets = crunch "assets"
Expand All @@ -8,7 +9,7 @@ let mollymawk =
package "logs";
package "x509";
package "tls-mirage";
package ~min:"2.1.0" "albatross";
package ~min:"2.1.0" ~max:"2.2.0" "albatross";
package "yojson";
package "uri";
package "tyxml";
Expand Down
109 changes: 58 additions & 51 deletions dashboard.ml
Original file line number Diff line number Diff line change
Expand Up @@ -461,57 +461,64 @@ let dashboard_layout (user : User_model.user) ~icon
[];
span [ txt "Marketplace" ];
];
hr ~a:[ a_class [ "my-4" ] ] ();
a
~a:
[
a_href "/admin/users";
a_class
[
"hover:bg-gray-200 hover:text-primary-400 \
font-semibold hover:font-bold \
cursor-pointer rounded p-2 w-full flex \
items-center space-x-1";
];
]
[
i
~a:
[
a_class
[
"fa-solid fa-users text-primary-500 \
text-sm";
];
]
[];
span [ txt "Users" ];
];
a
~a:
[
a_href "/admin/settings";
a_class
[
"hover:bg-gray-200 hover:text-primary-400 \
font-semibold hover:font-bold \
cursor-pointer rounded p-2 w-full flex \
items-center space-x-1";
];
]
[
i
~a:
[
a_class
[
"fa-solid fa-gears text-primary-500 \
text-sm";
];
]
[];
span [ txt "Settings" ];
];
(if user.super_user then
div
[
hr ~a:[ a_class [ "my-4" ] ] ();
a
~a:
[
a_href "/admin/users";
a_class
[
"hover:bg-gray-200 \
hover:text-primary-400 \
font-semibold hover:font-bold \
cursor-pointer rounded p-2 w-full \
flex items-center space-x-1";
];
]
[
i
~a:
[
a_class
[
"fa-solid fa-users \
text-primary-500 text-sm";
];
]
[];
span [ txt "Users" ];
];
a
~a:
[
a_href "/admin/settings";
a_class
[
"hover:bg-gray-200 \
hover:text-primary-400 \
font-semibold hover:font-bold \
cursor-pointer rounded p-2 w-full \
flex items-center space-x-1";
];
]
[
i
~a:
[
a_class
[
"fa-solid fa-gears \
text-primary-500 text-sm";
];
]
[];
span [ txt "Settings" ];
];
]
else div []);
];
];
section
Expand Down
36 changes: 32 additions & 4 deletions middleware.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ let redirect_to_register reqd ?(msg = "") () =
Httpaf.Reqd.respond_with_string reqd response msg;
Lwt.return_unit

let redirect_to_error ~title ~data status user code api_meth reqd () =
let error = { Utils.Status.code; title; success = false; data } in
let data =
if api_meth then Utils.Status.to_json error
else
Dashboard.dashboard_layout user ~page_title:(title ^ " | Mollymawk")
~content:(Error_page.error_layout error)
~icon:"/images/robur.png" ()
in
Lwt.return
(let headers =
Httpaf.Headers.of_list
[
("content-length", string_of_int (String.length data));
("content-type", if api_meth then "application/json" else "text/html");
]
in
let resp = Httpaf.Response.create ~headers status in
Httpaf.Reqd.respond_with_string reqd resp data)

let redirect_to_verify_email reqd ?(msg = "") () =
let headers = Httpaf.Headers.of_list [ ("location", "/verify-email") ] in
let response = Httpaf.Response.create ~headers `Found in
Expand Down Expand Up @@ -108,14 +128,22 @@ let auth_middleware now users handler reqd =
| Ok user ->
if user.User_model.active then handler reqd
else redirect_to_login ~msg:"User account is deactivated." reqd ()
| Error (`Msg msg) ->
Logs.err (fun m ->
m "auth-middleware: No molly-session in cookie header.");
redirect_to_login ~msg reqd ()
| Error (`Msg msg) -> redirect_to_login ~msg reqd ()

let email_verified_middleware now users handler reqd =
match user_of_cookie users now reqd with
| Ok user ->
if User_model.is_email_verified user then handler reqd
else redirect_to_verify_email reqd ()
| Error (`Msg msg) -> redirect_to_login ~msg reqd ()

let is_user_admin_middleware api_meth now users handler reqd =
match user_of_cookie users now reqd with
| Ok user ->
if user.User_model.super_user && user.active then handler reqd
else
redirect_to_error ~title:"Unauthorized"
~data:
"You don't have the necessary permissions to access this service."
`Unauthorized user 401 api_meth reqd ()
| Error (`Msg msg) -> redirect_to_login ~msg reqd ()
Loading

0 comments on commit 35db1c6

Please sign in to comment.