Iliad Framework, a status widget for displaying the current user

In the previous post we completed the users registration form. Now we can use the login form.

To show the current user in the next pages we will implement a current user heading as a widget:

<span class="nc">ILWidget</span> <span class="nf">subclass:</span> <span class="ss">#PnCurrentUserHeading</span>
    <span class="nf">instanceVariableNames:</span> <span class="s">''</span>
    <span class="nf">classVariableNames:</span> <span class="s">''</span>
    <span class="nf">poolDictionaries:</span> <span class="s">''</span>
    <span class="nf">category:</span> <span class="s">'LeonardoBlog'</span>

In the contents of this widget we will use the application method of the ILWidget class to access the current application and the current user:

<span class="k">!</span><span class="nc">PnCurrentUserHeading</span><span class="k"> methodsFor: </span><span class="s">'building'</span><span class="k">!</span>
<span class="nf">contents</span>
    <span class="o">^</span> [ <span class="o">:</span><span class="nv">e</span> <span class="o">|</span>
    <span class="nv">e</span> <span class="nf">div</span>
        <span class="nf">cssClass:</span> <span class="s">'navbar navbar-static-top bs-docs-nav'</span><span class="p">;</span>
        <span class="nf">build:</span> [ <span class="o">:</span><span class="nv">header</span> <span class="o">|</span>
                    <span class="nv">header</span> <span class="nf">div</span>
                        <span class="nf">class:</span> <span class="s">'navbar-brand'</span><span class="p">;</span>
                        <span class="nf">text:</span> <span class="s">'Project Notes for '</span> <span class="nf">,</span> <span class="bp">self</span> <span class="nf">application</span> <span class="nf">currentuser</span> <span class="nf">email</span><span class="p">.</span>
                    (<span class="nv">header</span> <span class="nf">ul</span>
                        <span class="nf">cssClass:</span> <span class="s">'nav navbar-nav'</span><span class="p">;</span>
                        <span class="nf">li</span>)
                        <span class="nf">build:</span> [ <span class="o">:</span><span class="nv">logout</span> <span class="o">|</span>
                            <span class="nv">logout</span> <span class="nf">a</span>
                                <span class="nf">text:</span> <span class="s">'Logout'</span><span class="p">;</span>
                                <span class="nf">action:</span> [ <span class="bp">self</span> <span class="nf">logout</span> ] ] ] ]

We also included a logout action that reset the current user and redirect the application to the login page:

<span class="k">!</span><span class="nc">PnCurrentUserHeading</span><span class="k"> methodsFor: </span><span class="s">'actions'</span><span class="k">!</span>
<span class="nf">logout</span>
    <span class="bp">self</span> <span class="nf">application</span> <span class="nf">currentuser:</span><span class="bp">nil</span><span class="p">.</span>
    <span class="bp">self</span> <span class="nf">redirectToLocal:</span> <span class="s">'login'</span><span class="p">.</span>

We include this widget in the application class like we have done before with the login and the registration page:

<span class="nc">ILApplication</span> <span class="nf">subclass:</span> <span class="ss">#LcBlogProjectNotes</span>
    <span class="nf">instanceVariableNames:</span> <span class="s">'loginWidget registrationWidget currentuser currentUserWidget'</span>
    <span class="nf">classVariableNames:</span> <span class="s">''</span>
    <span class="nf">poolDictionaries:</span> <span class="s">''</span>
    <span class="nf">category:</span> <span class="s">'LeonardoBlog'</span>

<span class="nf">!</span><span class="nc">LcBlogProjectNotes</span> <span class="nf">methodsFor:</span> <span class="s">'accessing'</span><span class="nf">!</span>
<span class="nf">currentUserWidget</span>
    <span class="o">^</span> <span class="nv">currentUserWidget</span> <span class="nf">ifNil:</span> [ <span class="nv">currentUserWidget</span> <span class="o">:=</span> <span class="nc">PnCurrentUserHeading</span> <span class="nb">new</span> ]

Now we create a new notes controller:

<span class="k">!</span><span class="nc">LcBlogProjectNotes</span><span class="k"> methodsFor: </span><span class="s">'controllers'</span><span class="k">!</span>
<span class="nf">notes</span>
    <span class="o">^</span> [ <span class="o">:</span><span class="nv">e</span> <span class="o">|</span> <span class="nv">e</span> <span class="nf">build:</span>(<span class="bp">self</span> <span class="nf">currentUserWidget</span>)<span class="p">.</span> ]