╭ Roles.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const inp = @import("../input.zig"); 6 const Comment = @import("./Comment.zig"); 7 const xit = @import("xit"); 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 15 const wasm = builtin.target.cpu.arch == .wasm32; 16 17 pub const page_size = 20; 18 19 // the repo's owners and collaborators, which only its owners see 20 identity: []const u8, 21 start: usize, 22 next_start: ?usize = null, 23 rows: []const Row, 24 // the role change the route asks to confirm, which stands in for the list 25 confirm: ?Confirm = null, 26 27 const Self = @This(); 28 29 pub const Confirm = struct { action: Action, name: []const u8 }; 30 31 pub const Row = struct { 32 name: []const u8, 33 role: evt.Repo.Role, 34 }; 35 36 pub const Action = enum { grant, revoke, promote, demote }; 37 38 // one window of the repo's grants, newest first, without read grants, the 39 // creator or removed users 40 pub fn init( 41 io: std.Io, 42 allocator: std.mem.Allocator, 43 arena: *std.heap.ArenaAllocator, 44 admin_moment: evt.AdminDB.HashMap(.read_only), 45 users_dir: []const u8, 46 identity: []const u8, 47 owner_id: *const [evt.event_id_size]u8, 48 repo_id: *const [evt.event_id_size]u8, 49 start: usize, 50 confirm: ?Confirm, 51 ) !Self { 52 const aa = arena.allocator(); 53 const grants = try evt.readRepoGrants(io, allocator, arena, users_dir, owner_id, repo_id); 54 var rows: std.ArrayList(Row) = .empty; 55 var skipped: usize = 0; 56 var next_start: ?usize = null; 57 for (grants) |grant| { 58 // the creator owns the repo with or without a grant 59 const user_id = try evt.parseEventId(&grant.user_id); 60 if (grant.role == .read or std.mem.eql(u8, &user_id, owner_id)) continue; 61 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, arena, &user_id)) orelse continue; 62 if (user.removed) continue; 63 if (skipped < start) { 64 skipped += 1; 65 continue; 66 } 67 if (rows.items.len == page_size) { 68 next_start = start + page_size; 69 break; 70 } 71 try rows.append(aa, .{ .name = user.event.name, .role = grant.role }); 72 } 73 return .{ 74 .identity = try aa.dupe(u8, identity), 75 .start = start, 76 .next_start = next_start, 77 .rows = rows.items, 78 .confirm = if (confirm) |value| .{ .action = value.action, .name = try aa.dupe(u8, value.name) } else null, 79 }; 80 } 81 82 // the confirmation of taking `name`'s role away 83 fn confirmRoute(identity: []const u8, start: usize, name: []const u8, action: Action) !ui.RoutablePage { 84 var route = ui.RoutablePage.repoRolesRoute(identity, start) orelse return error.RouteTooLong; 85 route.repo_roles.user = ui.RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 86 route.repo_roles.confirm = action; 87 return route; 88 } 89 90 // act on `name`'s role in the repo `identity` names as `actor`, one of its 91 // owners. returns why an add wrote nothing, or null once written 92 pub fn perform( 93 io: std.Io, 94 allocator: std.mem.Allocator, 95 admin_moment: evt.AdminDB.HashMap(.read_only), 96 users_dir: []const u8, 97 actor: ui.Actor, 98 identity: []const u8, 99 action: Action, 100 name: []const u8, 101 ) !?ui.Session.FormFeedback.RolesFailure { 102 const owner_id = actor.repo_user_id orelse return error.NotFound; 103 const parsed = ui.RoutablePage.RepoIdentity.parse(identity) orelse return error.NotFound; 104 const user_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, name)) orelse 105 return if (action == .grant) .not_found else error.EventNotFound; 106 107 var arena = std.heap.ArenaAllocator.init(allocator); 108 defer arena.deinit(); 109 const repo_id, const role = blk: { 110 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &owner_id)) orelse return error.NotFound; 111 defer user_repo.deinit(io, allocator); 112 const moment = (try evt.userMoment(&user_repo)) orelse return error.NotFound; 113 const found = (try evt.Repo.readByName(evt.UserDB, evt.user_repo_opts.hash, moment, &arena, parsed.name)) orelse return error.NotFound; 114 break :blk .{ found.event_id, try evt.Grant.readRole(evt.UserDB, evt.user_repo_opts.hash, moment, &arena, &found.event_id, &user_id) }; 115 }; 116 117 switch (action) { 118 // a reader becomes a collaborator; anyone else with a role, and the 119 // creator, who holds no grant, is already added 120 .grant => { 121 const already_added = if (role) |held| held != .read else std.mem.eql(u8, &user_id, &owner_id); 122 if (already_added) return .already_added; 123 try evt.grantRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, .write, actor.author); 124 }, 125 .revoke => try evt.revokeRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, actor.author), 126 // only a listed grant changes role 127 .promote, .demote => { 128 if (role == null) return error.EventNotFound; 129 try evt.grantRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, if (action == .promote) .owner else .write, actor.author); 130 }, 131 } 132 return null; 133 } 134 135 // where an action lands: the same window of the list, or the files tab once 136 // the actor gave up their own owner role 137 pub fn landingRoute(identity: []const u8, start: usize, actor: ui.Actor, action: Action, name: []const u8) ?ui.RoutablePage { 138 const lost_ownership = (action == .revoke or action == .demote) and std.mem.eql(u8, name, actor.author.name); 139 if (lost_ownership) return ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0); 140 return ui.RoutablePage.repoRolesRoute(identity, start); 141 } 142 143 pub const View = struct { 144 scroll: wgt.Scroll(ui.Widget), 145 data: *const Self, 146 session: *ui.Session, 147 148 const add_index = 0; 149 150 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 151 const aa = session.page_arena.allocator(); 152 if (data.confirm) |confirm| { 153 const url = try (try confirmRoute(data.identity, data.start, confirm.name, confirm.action)).toUrl(session.page_arena); 154 var center = try ui.widget.initConfirmationForm(allocator, try aa.print("form:{s}", .{url}), &.{.{ 155 .label = if (confirm.action == .demote) "demote to collaborator" else "remove user", 156 .bottom_label = try aa.print(" {s} ", .{confirm.name}), 157 }}); 158 errdefer center.deinit(allocator); 159 return .{ 160 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 161 .data = data, 162 .session = session, 163 }; 164 } 165 166 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 167 errdefer box.deinit(allocator); 168 const route = ui.RoutablePage.repoRolesRoute(data.identity, data.start) orelse return error.RouteTooLong; 169 const url = try route.toUrl(session.page_arena); 170 171 { 172 var add = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 173 errdefer add.deinit(allocator); 174 add.getFocus().kind = .{ .custom = try aa.print("form:{s}/grant", .{url}) }; 175 var name = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " add a collaborator " }, .name = "name", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 176 errdefer name.deinit(allocator); 177 name.getFocus().mode = .all; 178 if (session.formFeedback(.repo_roles)) |saved| try name.setContent(allocator, saved.name); 179 add.getFocus().child_id = name.getFocus().id; 180 try add.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_input = name }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 181 try box.children.put(allocator, add.getFocus().id, .{ .widget = .{ .box = add }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 182 } 183 184 // the user rows follow the add row in the same box 185 if (data.start > 0) { 186 var previous = try Comment.linkBox(allocator, session, "← previous", ui.RoutablePage.repoRolesRoute(data.identity, data.start -| page_size) orelse return error.RouteTooLong); 187 errdefer previous.deinit(allocator); 188 try box.children.put(allocator, previous.getFocus().id, .{ .widget = .{ .text_box = previous }, .rect = null, .min_size = null }); 189 } 190 for (data.rows) |row| { 191 var row_route = route; 192 row_route.repo_roles.user = ui.RoutablePage.Array(evt.User.name_max_len).from(row.name) orelse return error.RouteTooLong; 193 const row_url = try row_route.toUrl(session.page_arena); 194 195 var row_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 196 errdefer row_box.deinit(allocator); 197 row_box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{row_url}) }; 198 { 199 const user_route = ui.RoutablePage{ .user_repos = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(row.name) orelse return error.RouteTooLong } }; 200 var name = try Comment.linkBox(allocator, session, row.name, user_route); 201 errdefer name.deinit(allocator); 202 name.options.bottom_label.text = if (row.role == .owner) " owner " else " collaborator "; 203 try row_box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_box = name }, .rect = null, .min_size = null }); 204 } 205 // taking a role away asks first 206 if (row.role == .owner) { 207 const demote_route = try confirmRoute(data.identity, data.start, row.name, .demote); 208 try addButton(allocator, &row_box, "demote to collaborator", try aa.print("a:{s}", .{try demote_route.toUrl(session.page_arena)})); 209 } else try addButton(allocator, &row_box, "promote to owner", try aa.print("submit:{s}/promote", .{row_url})); 210 { 211 const revoke_route = try confirmRoute(data.identity, data.start, row.name, .revoke); 212 var remove = try wgt.TextBox.init(allocator, "✕", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 213 errdefer remove.deinit(allocator); 214 remove.getFocus().mode = .all; 215 remove.getFocus().kind = .{ .custom = try aa.print("a:{s}", .{try revoke_route.toUrl(session.page_arena)}) }; 216 try row_box.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 217 } 218 row_box.getFocus().child_id = row_box.children.keys()[0]; 219 try box.children.put(allocator, row_box.getFocus().id, .{ .widget = .{ .box = row_box }, .rect = null, .min_size = null }); 220 } 221 if (data.next_start) |next_start| { 222 var next = try Comment.linkBox(allocator, session, "next →", ui.RoutablePage.repoRolesRoute(data.identity, next_start) orelse return error.RouteTooLong); 223 errdefer next.deinit(allocator); 224 try box.children.put(allocator, next.getFocus().id, .{ .widget = .{ .text_box = next }, .rect = null, .min_size = null }); 225 } 226 227 box.getFocus().child_id = box.children.keys()[add_index]; 228 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 229 errdefer center.deinit(allocator); 230 return .{ 231 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 232 .data = data, 233 .session = session, 234 }; 235 } 236 237 fn addButton(allocator: std.mem.Allocator, row: *wgt.Box(ui.Widget), text: []const u8, kind: []const u8) !void { 238 var button = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 239 errdefer button.deinit(allocator); 240 button.getFocus().mode = .all; 241 button.getFocus().kind = .{ .custom = kind }; 242 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 243 } 244 245 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 246 self.scroll.deinit(allocator); 247 } 248 249 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 250 return &self.scroll.child.center.child.box; 251 } 252 253 fn addBox(self: *View) *wgt.Box(ui.Widget) { 254 return &self.contentBox().children.values()[add_index].widget.box; 255 } 256 257 fn nameInput(self: *View) *wgt.TextInput { 258 return &self.addBox().children.values()[0].widget.text_input; 259 } 260 261 fn addActive(self: *View) bool { 262 return self.contentBox().getFocus().child_id == self.addBox().getFocus().id; 263 } 264 265 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 266 self.clearGrid(); 267 if (self.data.confirm == null) try self.labelAddInput(root_focus); 268 // the window's height as a minimum centers rows that fit 269 try self.scroll.build(allocator, .{ 270 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 271 .max_size = constraint.max_size, 272 }, root_focus); 273 } 274 275 // label the add input for this frame and register it for the web form 276 // handling 277 fn labelAddInput(self: *View, root_focus: *Focus) !void { 278 const name = self.nameInput(); 279 const failure = if (self.session.formFeedback(.repo_roles)) |saved| saved.failure else null; 280 name.options.top_label.text = if (failure) |value| switch (value) { 281 .not_found => " add a collaborator (not found) ", 282 .already_added => " add a collaborator (already added) ", 283 } else " add a collaborator "; 284 name.options.bottom_label.text = if (root_focus.grandchild_id == name.getFocus().id) " press enter " else ""; 285 try self.session.text_inputs.put(self.session.arena.allocator(), name.getFocus().id, name); 286 } 287 288 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 289 if (self.data.confirm) |confirm| { 290 const button_id = self.contentBox().getFocus().child_id orelse return; 291 if (inp.activated(root_focus, button_id, key)) try self.act(allocator, confirm.action, confirm.name); 292 return; 293 } 294 // the add row keeps every key but the vertical ones, which move between rows 295 if (self.addActive() and inp.vertDirection(key) == .none) { 296 if (key == .enter) { 297 const name = try self.nameInput().text(allocator); 298 defer allocator.free(name); 299 try self.act(allocator, .grant, name); 300 } else try self.nameInput().input(allocator, key, root_focus); 301 return; 302 } 303 304 const rows = self.contentBox(); 305 const current = rows.children.getIndex(rows.getFocus().child_id orelse return) orelse return; 306 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 307 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 308 return; 309 } 310 switch (key) { 311 .arrow_left, .arrow_right => _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right), 312 else => { 313 // the add row and the previous link, when there is one, sit above the rows 314 const offset: usize = add_index + 1 + @intFromBool(self.data.start > 0); 315 if (current < offset or current - offset >= self.data.rows.len) return; 316 const row = self.data.rows[current - offset]; 317 const row_box = &rows.children.values()[current].widget.box; 318 // remove and demote are links to their confirmations, which the host follows 319 if (row.role == .owner) return; 320 const button_id = row_box.getFocus().child_id orelse return; 321 if (row_box.children.getIndex(button_id) != 1 or !inp.activated(root_focus, button_id, key)) return; 322 try self.act(allocator, .promote, row.name); 323 }, 324 } 325 } 326 327 // the terminal performs the action itself; the web posts the form instead 328 fn act(self: *View, allocator: std.mem.Allocator, action: Action, name: []const u8) !void { 329 if (comptime wasm) return; 330 const io = self.session.io orelse return; 331 const users_dir = self.session.users_dir orelse return; 332 const admin_repo = self.session.admin_repo orelse return; 333 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 334 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 335 const failure = perform(io, allocator, moment, users_dir, actor, self.data.identity, action, name) catch |err| switch (err) { 336 // a stale row; the refresh below shows the list as it is 337 error.EventNotFound => null, 338 else => return err, 339 }; 340 if (failure) |value| { 341 self.session.data.form_feedback = .{ .repo_roles = .{ .failure = value, .name = try self.session.arena.allocator().dupe(u8, name) } }; 342 return; 343 } 344 try self.session.navigate(landingRoute(self.data.identity, self.data.start, actor, action, name) orelse return error.RouteTooLong); 345 } 346 347 pub fn clearGrid(self: *View) void { 348 self.scroll.clearGrid(); 349 } 350 351 pub fn getGrid(self: View) ?Grid { 352 return self.scroll.getGrid(); 353 } 354 355 pub fn getFocus(self: *View) *Focus { 356 return self.scroll.getFocus(); 357 } 358 359 // up leaves the view from the add row or the confirmation 360 pub fn atTop(self: *View) bool { 361 return self.data.confirm != null or self.addActive(); 362 } 363 }; 364