| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using Astralis;
- using Inversion;
- using Spry.Authorisation;
- namespace Spry.Authentication {
- public class UserManagementComponent : Component {
- public int page_number { get; set; default = 0; }
- public string authorisation_permission { get; set; default = "spry.admin"; }
- public bool authorised { get; set; }
- public const int64 users_per_page = 20;
- public override string markup { get { return """
- <spry-context property="page_number" />
- <div spry-if="!this.authorised">
- <strong>You must have the permission <code content-expr="this.authorisation_permission"></code> to access this content.</strong>
- </div>
- <div spry-else sid="container" style="gap: 1em;">
- <spry-outlet sid="outlet" />
- <div>
- <button spry-action=":previous" spry-target="container">Previous</button>
- <span>Page <strong content-expr="stringify(this.page_number + 1)"></strong></span>
- <button spry-action=":next" spry-target="container">Next</button>
- </div>
- </div>
- """; }}
- private UserService user_service = inject<UserService>();
- private AuthorisationContext authorisation_context = inject<AuthorisationContext>();
- private ComponentFactory component_factory = inject<ComponentFactory>();
- public async override void prepare () throws Error {
- if(authorisation_context.is_anonymous() || !authorisation_context.has_permission (authorisation_permission)) {
- authorised = false;
- return;
- }
- authorised = true;
- var users = yield user_service.list_users (page_number * users_per_page, page_number * users_per_page + users_per_page);
- foreach (var user in users) {
- var component = component_factory.create<UserComponent>();
- component.user_id = (int)user.id;
- component.user = user;
- add_outlet_child ("outlet", component);
- }
- }
- public async override void handle_action(string action) {
- if(action == "previous") {
- page_number --;
- if(page_number < 0) {
- page_number = 0;
- }
- }
- if(action == "next") {
- page_number ++;
- }
- }
-
- }
- }
|