From 3a7e58abca6bc520c851612a1eea765b91e2dd03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Mon, 8 Nov 2021 16:51:16 +0100 Subject: [PATCH] Add builder-db access-list command --- bin/builder_db.ml | 20 ++++++++++++++++++-- db/builder_db.ml | 10 ++++++++-- db/builder_db.mli | 4 +++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/bin/builder_db.ml b/bin/builder_db.ml index 5630dc5..ce9e4a0 100644 --- a/bin/builder_db.ml +++ b/bin/builder_db.ml @@ -71,7 +71,7 @@ let user_list () dbpath = connect (Uri.make ~scheme:"sqlite3" ~path:dbpath ~query:["create", ["false"]] ()) in Db.iter_s Builder_db.User.get_all - (fun username -> Ok (print_endline username)) + (fun (_id, username) -> Ok (print_endline username)) () in or_die 1 r @@ -136,6 +136,17 @@ let access_remove () dbpath username jobname = in or_die 1 r +let access_list () dbpath = + let r = + let* (module Db : Caqti_blocking.CONNECTION) = + connect (Uri.make ~scheme:"sqlite3" ~path:dbpath ~query:["create", ["false"]] ()) + in + Db.iter_s Builder_db.Access_list.get_all_names + (fun (username, job) -> Ok (Printf.printf "%s:%s\n" username job)) + () + in + or_die 1 r + let job_remove () datadir jobname = let dbpath = datadir ^ "/builder.sqlite3" in let r = @@ -432,6 +443,11 @@ let access_remove_cmd = (Cmdliner.Term.(pure access_remove $ setup_log $ dbpath $ username $ job), Cmdliner.Term.info ~doc "access-remove") +let access_list_cmd = + let doc = "list user access" in + (Cmdliner.Term.(pure access_list $ setup_log $ dbpath), + Cmdliner.Term.info ~doc "access-list") + let job_remove_cmd = let doc = "remove job and its associated builds and artifacts" in (Cmdliner.Term.(pure job_remove $ setup_log $ datadir $ jobname), @@ -467,6 +483,6 @@ let () = default_cmd [help_cmd; migrate_cmd; user_add_cmd; user_update_cmd; user_remove_cmd; user_list_cmd; user_disable_cmd; - access_add_cmd; access_remove_cmd; job_remove_cmd; + access_add_cmd; access_remove_cmd; access_list_cmd; job_remove_cmd; verify_input_id_cmd; verify_data_dir_cmd ] |> Cmdliner.Term.exit diff --git a/db/builder_db.ml b/db/builder_db.ml index f0e1257..7a0e1cf 100644 --- a/db/builder_db.ml +++ b/db/builder_db.ml @@ -588,8 +588,8 @@ module User = struct let get_all = Caqti_request.collect Caqti_type.unit - Caqti_type.string - "SELECT username FROM user" + Caqti_type.(tup2 (id `user) string) + "SELECT id, username FROM user" let add = Caqti_request.exec @@ -644,6 +644,12 @@ module Access_list = struct (id `access_list) "SELECT id FROM access_list WHERE user = ? AND job = ?" + let get_all_names = + Caqti_request.collect + Caqti_type.unit + Caqti_type.(tup2 string string) + "SELECT username, name FROM access_list acl LEFT JOIN user, job ON user.id = acl.user AND job.id = acl.job" + let add = Caqti_request.exec Caqti_type.(tup2 (id `user) (id `job)) diff --git a/db/builder_db.mli b/db/builder_db.mli index bae5eae..a9f7c4f 100644 --- a/db/builder_db.mli +++ b/db/builder_db.mli @@ -163,7 +163,7 @@ module User : sig [< `Many | `One | `Zero > `One `Zero ]) Caqti_request.t val get_all : - (unit, string, [ `Many | `One | `Zero ]) Caqti_request.t + (unit, [`user] id * string, [ `Many | `One | `Zero ]) Caqti_request.t val add : (Builder_web_auth.scrypt Builder_web_auth.user_info, unit, [< `Many | `One | `Zero > `Zero ]) Caqti_request.t @@ -177,6 +177,8 @@ end module Access_list : sig val get : ([`user] id * [`job] id, [`access_list] id, [< `Many | `One | `Zero > `One ]) Caqti_request.t + val get_all_names : + (unit, string * string, [ `Many | `One | `Zero ]) Caqti_request.t val add : ([`user] id * [`job] id, unit, [< `Many | `One | `Zero > `Zero ]) Caqti_request.t val remove :