nextcloud / cms_pico

🗃 Integrate Pico CMS and let your users manage their own websites
https://apps.nextcloud.com/apps/cms_pico
GNU Affero General Public License v3.0
134 stars 42 forks source link

Added User session to meta #229

Closed mexthecat closed 1 year ago

mexthecat commented 1 year ago

I have added the current user id to the meta info.

This allows me to add an Login Button on the Webseit if a user is not logged in to nextcloud.

I'm using it to show restricted content if a user is logged in.

<header class="site-header sticky-top">
  <nav class="navbar navbar-light navbar-expand-md bg-light" id="mainNav">
    <div class="container-fluid">
      <a class="navbar-brand" href="{{ "index"|link }}"><img src="{{ theme_url }}/assets/img/logo.svg"></a>
      <button data-bs-toggle="collapse" class="navbar-toggler" data-bs-target="#navcol-1">
        <span class="visually-hidden">Toggle navigation</span>
        <span class="navbar-toggler-icon"></span></button>
      <div class="collapse navbar-collapse" id="navcol-1">
        <ul class="navbar-nav  me-auto">
          {% for page in pages(depthOffset = -1) if page.title and not page.hidden %}
            <li class="nav-item">
              <a class="nav-link" href="{{ page.url }}">{{ page.title }}</a>
            </li>
          {% endfor %}
          {% if meta.user == "" %}
            <li class="nav-item">
              <a class="nav-link" href="/index.php/s/o7qRnkdFSQ4CyK6">Public Info</a>
            </li>
          {% elseif meta.user %}
            <li class="nav-item">
              <a class="nav-link" href="/index.php/f/82572">Private Info</a>
            </li>
          {% endif %}
        </ul>
        <div>
        {% if meta.user == "" %}
            <a class="nav-link" href="/index.php/login?redirect_url={{ "index"|link }}"><button class="btn btn-primary" type="button">Login</button></a>
        {% elseif meta.user %}
            <a class="nav-link" href="/index.php/u/{{ meta.user }}"><button class="btn btn-primary" type="button">{{ meta.user }}</button></a>
        {% endif %}
        </div>
      </div>
    </div>
  </nav>
</header>

were /index.php/s/o7qRnkdFSQ4CyK6 is a public link to a pdf file and /index.php/f/82572 is a private on.

Maybe there is an other way to solve this ...

github-actions[bot] commented 1 year ago

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! :+1:

PhrozenByte commented 1 year ago

Thank you for your contribution :heart:

Passing some Nextcloud metadata (like the current user) to theme developers is a good idea for sure, we should definitely implement this! :+1: However, I've some thoughts about the implementation. We shouldn't use meta for this, since meta stores a page's metadata, i.e. its YAML Front Matter. For Nextcloud metadata we should use something else.

I'd suggest adding a new Twig global variable nextcloud instead - this also makes it easy to detect whether a theme is being used with Pico CMS for Nextcloud, or pure Pico (e.g. {% if nextcloud %}). Furthermore I wouldn't limit this to the user, there surely is more useful metadata. Right now I can think of Nextcloud's start page URL (usually the dashboard app) next to the user ID, but there surely is more. Some input from others is very welcome, we can add more variables at any time.

Some more notes about the implementation: Please remove the pico_cms_public_user config, just leave it null then. And we shouldn't pass IUserSession down to Pico via PicoService, rather use \OC::$server->query(IUserSession::class) inside Pico::__construct() instead. Pico uses no DI, we shouldn't force it here.

To get this merged later, please also check our CONTRIBUTING.md - especially the DCO - and sign off your commits.

github-actions[bot] commented 1 year ago

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! :+1: