Dynamic html content depending on user's role - backbone.js

I am developing single page application style web application.
I want to create menu based on user's role, for example, for admin users I want show admin menu and html content, for general users I want to show different html content.
I am using asp.net mvc with Razor view engine, so this could be achieved by Razor code as follow :
#if (#User.IsInRole("Admin"){
-- html content for admin --
}else if (#User.IsInRole("General"){
-- html content for general user --
}else{
-- html content for the other users --
}
This was the typical way when I was developing web application in traditional way in the past, however since I started developing single page application style web application, I tried to write pure html code and avoided using server side view engile such as Razor, WebForm in asp.net or Velocity, Jsp tag library in Java EE. I want to write more independent front-end code without server code dependency.
Anyhow, above code can be changed as follow when I use javascript and knockoutjs instead of Razor,
<!-- ko if: isAdmin -->
-- html content for admin --
<!-- /ko -->
<!-- ko if: isGeneralUser -->
-- html content for general user --
<!-- /ko -->
<!-- ko if: isOtherUser -->
-- html content for the other users --
<!-- /ko -->
isAdmin, isGeneralUser and isOtherUser are properties of viewmodel and these values will be set via ajax call which check user's role.
What I am wondering is whether this sort of way to controll content upon user's role is fine or not from security's perspective?
How do you handle view content if those views are different depending on user's role?
I would like to how do you guys handle this kind of situation.

I would use a template binding
<div data-bind="template: { name: template, data: model }"></div>
On the viewmodel set the correct template and model depending on User role, also, you still need to protect the role specific methods server side
A little side note, I've done a little framework for Convention bound templates, its perfect for scenarios like this, its not done yet so I would wait to use it in a production site, but it will be ready in the coming weeks
The Template binding above would with my framework look like
<div data-bind="coc: model"></div>
If the ViewModel is named AdminViewModel than my library will look for a templte named AdminView
A little fiddle http://jsfiddle.net/2Uvd5/1/
edit: Fore a more accurate example on what you wanna do
http://jsfiddle.net/XaZxj/

Related

Django CMS only one entry in model

I want to make a persistent floating(using CSS) contact button on every page in Django CMS.
I need to get the first item in DB and construct the URL according to it in the template.
Also, the place holder should display first item and allow editing it everywhere if possible.
Or maybe not have an input at all and allow editing the contact info in the admin.
Or have an option to use the first one which is default or a different number for a particular page.
I have currently made my plugin but it creates a new entry every time.
If you want something to appear on every page you need to make use of static_placeholder (docs) which acts like a normal placeholder but you only need to add plugins to it on one page for them to appear in every page which you include the static_placeholder of the same name.
For example I often have a footer which includes a menu & allows a client to add their own content like contact address, phone number etc. The easiest example of how you could do this would be to include the following template tag in your base.html so that it gets included in all pages inheriting your base template.
{% static_placeholder "footer" site %}
Also the standard placeholder tag doesn't work in apps, so if you include apps in your CMS site via app hooks you can also include the static_placeholder in the app templates to add plugins.

How do I modify Drupal's Admin Screens?

I'm looking to edit Drupal's admin pages.
Some of the things I'm trying to achieve:
Remove is the the filter setting on the Content page/node/view. In other words, if I go to to the Content page and I filter by a certain type of node, then I leave that page, then come back, the filter setting is saved, I'd like it to reset each time I visit the Content page.
I would like to modify some of links associated with these pages, as an example on that Same Content page, the titles of the pages are linked to their 'front-end' views, I'd rather have them link to their 'edit' screens.
While I have been successful in modifying the front-end theme of Drupal, by editing the actual php files. I can't find the the same php files for the Admin theme. In our case we are using Seven (7.21). I've read something about views, and thought maybe the entire Admin theme is built as views, aka there no php files involved like the front-end theme has, but under the Views section I did not find any either. I'm still pretty new at this and coming from the Wordpress world, this is a Drupal is a lot different.
The logic for that section of the admin area is at (from the public root):
/modules/node
Specifically "node.admin.inc"
That being said, I don't recommend editing core scripts, as they will be overwritten when you update the core.
You could explore making your own module to supplement that page, or your own view or explore some pre-made modules, such as "Admin Views"
Find de view displayed and go to its configuration :
1 - Edit configuration view like suggested here : https://drupal.stackexchange.com/questions/44440/how-to-remember-exposed-filter-choices
2 - Rewrite output views to rewrite link : field https://www.drupal.org/node/1578524
Another option is to create and use your own dashboard/views to achive like you want http://definitivedrupal.org/suggestions/creating-custom-administrator-view-content
also you can see contrib module : https://www.drupal.org/project/admin_views
You don't have to use that default content overview page at all. Make your own instead:
Make a page view (you have to install views module if you already did not: https://www.drupal.org/project/views) and use table layout. Make sure that page paths starts with "admin" so it will use admin theme. Also pay attention about permissions, so only specific set of users (admins) can use it.
For that view you can freely select what columns you what to show. You can also allow ordering by any column and action if you click on some field.
Use exposed filters to add filtering form, again add any filters you need. You can even set the form to use AJAX, to do auto-submit when some filter field is changed, include reset button and more. Also for every exposed filed you can select (at fields settings) should it remember it's value or not.
At end, edit admin menu and add your new page to it so it will look and act as default one but will offer much more functionality. You can even totally remove default one from the admin menu.
Views module is very powerful, not only for front-end but also for back-end content handling.

How to modify HTMLElement in index.html before page gets returned to requestor

Based on my custom URL parameters I process, I am trying to modify dynamically a meta tag I have id'ed in index.html like so:
<meta name="og:image" content="http://example.com/someurl.jpg" id="ogImage"/>
The code below in my home.ts seems to be working
document.getElementById('ogImage').setAttribute("content", Media.ImageURL) ;
I can verify it is via the browser dev console/elements.
However, when I view from facebook via their ojbect graph debugger at
https://developers.facebook.com/tools/debug/og/object/
It appears to see the default
http://example.com/someurl.jpg
as if the index.html is shipped before my home.ts gets chance to make the update.
Perhaps, my understanding is flawed and there is better way to do this.
Thank you.
Note1: initially, I was thinking I had to make some angular binding between index.html and one of my services but I could not locate any sample code, the closest I came to was this post
How can I update meta tags in AngularJS?
But I don't know how to apply it for my ionic2/3 code, so I opted for the document.get approach.
Note2: the ultimate goal here is to share a link into a social media (web or app) like facebook, a messenger like viber/skype, etc... and have it resolve to meaningful images, title, description to drive the visit back to the site via browser, or app if the user clicking on the link is on a mobile device with my app version of the site installed on his device.
Note3: if you decide to point me to ionic deeplinking please provide code to match above, because I could not understand how to apply to my case.
If you are trying to implement dynamic open graph meta tags values in your pages, you will need a server-side scripting language like php. Such a script will run on the server, update the pages as needed, then the pages will be served to the requesting site or application.
client-side scripting (ie. JavaScript) is usually ignored when a site or app is merely visiting your site/link for the purpose of extracting (aka scrapping, parsing html) information such as the one provided by the open graph meta tags (og:title, og:description og:image...).

Internationalization of Dynamic content using spring or angularjs

I need a help on doing a internationalization of dynamic content. Let's suppose I have a application which is working in different region like jersey, Philippines, Argentina etc. I need to show content based on the country user's login. I already know how to show it for labels. But for dynamic content, I unable to think how to proceed. For eg. if a user login in a particular region, I need to show his name in that particular region language or If he changes his language preference then need to show based on the selected preference.
I am using spring and angularjs in my application.
How to proceed for this?
I made one angular application with just the requirement you said. i18n and l10n.
angular-translate for dynamic label translations
angular-i18n for localization.
angular-dynamic-locale for dynamic localization.
EDIT EXAMPLE
Simple page http://the-unsoul.github.io/angular-requireJS/#/home (only couple fields got its label tranlating)
Source code https://github.com/the-unsoul/angular-requireJS

Durandal Mobile Views

Does anyone have a solution that will allow you to create a desktop and mobile view for a single viewmodel in Durandal?
Ideally I would like to take a page from MVC4 and have a view.html and view.mobile.html for each viewmodel and just direct the browser to the relevant one based on userAgent string.
Edit 1
Looks like I can override the View Locator's convertModuleIdToViewId function, solution to follow.

Resources