How to hide link by guard angular2 - angular2-routing

How to hide admin link in view html. I have to guard :Admin and Manager
Router config :
{
path: 'manager',
component: ManagerComponent,
canActivate: [ManagerGuard]
},
{
path: 'user',
component: UserAdminComponent,
canActivate: [AdminGuard]
}
In view :
<li>
<a routerLink="/user" routerLinkActive="active-link">User</a>
</li>
I want to hide link on /user for ManagerGuard when but show for AdminGuard.

Also if already answered in a way which might be helpful, I used here another approach. As you already have your canActivate method in the Guard, you could also inject your navigation component with that Guards and you can call these canActivate methods directly:
In your component which holds the navigation
constructor(private adminGuard: AdminGuard)
and then in the template
<li *ngIf="adminGuard.canActivate()">
<a routerLink="/user" routerLinkActive="active-link">User</a>
</li>
Edit
This does not work in prod mode in my case (if you use parameters which have to be injected). I just tried to compile it but angular complains about the missing parameters. If you do not use parameters, this works fine, or, if you do not use the parameters in your function - then simply pass
<li *ngIf="adminGuard.canActivate(null,null)">
<a routerLink="/user" routerLinkActive="active-link">User</a>
</li>
Another thing: If you is variables like the 'adminGuard' above in the template, it must be public - not private.

In my opinion this has nothing to do with the router itself.
Your Guards may call another service which has the information about which kind of user is logged in: Admin or Manager.
You should create a service that knows the type of the user. Then inject this service with Dependency Injection into your component where you have the routerLink.
There you can ask the service and toggle the link with *ngIf:
<li *ngIf="myService.getCurrentUser().isAdmin()">
<a routerLink="/user" routerLinkActive="active-link">User</a>
</li>
So the service provides a function that gives you the user which is currently logged in and the isAdmin()-function of the user returns true or false. If the user is admin then the <li> will be shown, otherwise it will be hidden.
This is just an example but I hope you get the point. It has more to do with basic functionality of Angular 2 rather than the router of Angular 2. More information about *ngIf can be found here.

Related

Spring MVC - Share Data

I'm new to Spring and I've been using Spring boot with Freemarker template to buld a simple blog application. In my application I have a navbar.ftl which is shared by all other views.
In this nav bar I want to access a list of all Authors in order to show their names in a drop down (code below), but I'm not sure how to access it here, since this navbar is shared between all views.
<li class="dropdown">
Authors <span class="caret"></span>
<ul class="dropdown-menu">
<#--Need to list all authors here. How???-->
</ul>
</li>
I have an AuthorRepository where I can find all authors, but I'm not sure how to use it in my view (I don't think the view could access the model directly anyway).
#Repository
public interface AuthorRepository extends CrudRepository<Author, Long> {
}
Any idea how I could have a list of authors in this navbar so I can show their names?
I think controller you can create function common ShareData. Put all data for method, when need you use this with call function ShareData(). That's all I thought.

Template.body.helpers returns no value

As my ROOT_URL differs in Dev and Prod environments I am rtying to use the Meteor.settings functionality to set my href values correctly.
Because links are used all over the application, it seemed a natural candidate for a helper on the body template. It is defined like so:
Template.body.helpers({
rootURL: Meteor.settings.public.root_url
})
and in the template it is used like so:
<li class="{{guessesActive}}">
<a href="{{rootURL}}guesses" class="waves-effect waves-light">Guesses
<span class="sr-only">(current)</span>
</a>
</li>
But the value of Meteor.settings.public.root_url is not appended to the anchor.
I think you just need to add as a registered helper instead of appending to the body. Template.registerHelper('rootURL', function() {...}); Then return the appropriate value. Meteor docs have more on registerHelper.
It turns out I misunderstood the body template. I thought it was meant to be accessible for all. It runs out this is not the case. It is meant to allow using helpers directly in the <body> tag.
To set global helpers, define a global variable called Helpers and assign it an object with the functions and variables you want accessible.
As explained in this post https://stackoverflow.com/a/28837834/763962

Symfony 2 : HWiOauthBundle : Overriding connect_success.html.twig

I have implemented both FOSUserBundle and HWIOauthBundle in my Symfony 2 project. Basically, everything works fine but I would like to customize a litle bit more. Typically I use the connect functionnality of the HWIOauthBundle to connect oauth account to user already connected with the authentication form (FOSUserBundle).
In case of succes, the controller action HWIOAuthBundle:Connect:connectService displays the twig template connect_success.html.twig. At this point, I would like to override this template and do the following actions :
Create a flash message
Display the flash message on my homepage
You can obtain this behavior easily with FOSUserBundle as this bundle dispatchs many events to hook into the controllers. But with HWIOauthBundle this is not possible.
My solution is the following :
1/ I override connect_success.html.twig by placing the same named file in app/Ressources/HWIOauthBundle/views/Connect with the following code:
{{ render(controller('MyUserBundle:User:HWIOAuthFlash')) }}
2/ In my user controller (MyUserBundle:User), I create an action HWIOAuthFlashAction() that defines a flash message and forwards to the controller action that displays he homepage(MyMainBundle:Main:homepage)
public function HWIOAuthFlashAction()
{
// Here : flash message definition
return $this->forward('MyMainBundle:Main:homepage');
}
At this point the homepage is displayed with the flash message. But I had to remove two links in the homepage template (homepage.twig.html)that permits the user to switch between two locales.
The following code is the one that I had to remove from my template :
<ul>
<li>FR</li>
<li>EN</li>
</ul>
I understand that that the special variable _route is null. And I get the following message :
An exception has been thrown during the rendering of a template
("Error when rendering
"xxxxx/web/app_dev.php/connect/service/google?key=yyyyyyy" (Status
code is 500).") in HWIOAuthBundle:Connect:connect_success.html.twig at
line 3.
I have two questions :
To achieve my goal, is it the good way ?
How to say to symfony that I want my route to be homepage ?
As proposed in my comment, here is my solution :
In my homepage template homepage.twig.html :
{% set route = app.request.get('_route') ? app.request.get('_route') : 'homepage' %}
<ul>
<li>FR</li>
<li>EN</li>
</ul>

Repository is Nothing but added in the container later (correctly) (using Unity, Repositories) in ASP.Net MVC 3

I have a "MenuService", which uses an "MenuRepository".
When the MenuService is created, it loads correctly with the MenuRepository.
Then it wants to save it in the UnityControllerFactory, but the MenuRepository in the MenuService is Nothing then (it wasn't before).
Very strange.
Does anyone has an idea why this is happening?
Some additional info:
I'm using the Unity and Repository pattern.
The problem happens with a partial view (for a menu), which i have included in the "HomeController", it could be relevant information, so i included this, just in case:
Function Menu() As ActionResult
Return PartialView("~\Views/Shared/_Menu.vbhtml", _MenuService.GetAllMenuItems)
End Function
Also, my partial "menu" view is like this:
~/_Menu.vbhtml
#ModelType IEnumerable(Of FacturatieMVCv2.Domain.Slave.MenuItem)
#*<div id="myslidemenu" class="jqueryslidemenu">*#
<ul>
#For Each Item In Model.Where(Function(el) IsNothing(el.HasHigherMenuItem))
#<li> #Html.ActionLink(Item.Naam,Item.Action,Item.Controller)
<ul>
#For Each SubItem In Model.Where(Function(el) el.HasHigherMenuItem.MenuItemID.Equals(Item.MenuItemID))
#<li>
#Html.ActionLink(SubItem.Naam,SubItem.Action,SubItem.Controller)
</li>
Next
</ul>
</li>
Next
</ul>
And i'm calling the partial view with:
#Html.Action("Menu","Home")
You need to setup your IoC before your application starts and you need to get your service from IoC.
This article will help you a lot.
I this case repository dependencies in the service will be resolved by IoC (Unity)

Connecting An <a> tag to a ViewController mvc

I know this is probably straight forward, but this isnt working for me, I want to simply connect this to trigger a viewController when I click it. When I create a controller and a view and click it it throws a 404 and the code never hits the controller.
<div> All Accounts In the System</div>
You should probably follow few tutorials for mvc. Links are by default using System.Web.Routing magic (if you have default template, look into global.asax). Default url looks like /controller/action/id where id is optional, action defaulted to Index and controller defaulted to Home. For link construction in views use these helper methods:
#Html.ActionLink("All accounts in the system", "AdminReports", "Admin");
or
All accounts in the system
The link should look like /Admin/AdminReports with default settings.
You want something like this:
<a href='<%= Url.Action("Controller", "Action") %>'>Text</a>
where
Controller = Your controller name.
Action = Your action method.

Resources