This project is read-only.

Real(Roles) + Visible in Menu

Topics: Developer Forum, Localization Forum, Project Management Forum, User Forum
Nov 16, 2007 at 6:12 PM
Edited Nov 16, 2007 at 6:13 PM
Does someone manage a solution for roles and visibility in menu both working toghether.
Nov 29, 2007 at 8:26 AM
I am also looking for a solution to this. I want my users to upload files and have those files available only to that user.

  • I have two problems:
    • The first is that there is no file upload easy control but I think I can create this. I'm currently using the gallery control for the file upload but would like a seperate file upload page. Then I would allow users to retreive their files from the gallery. This won't be feasable in my finnished site because not all the files will be in picture format.
    • The second case is that the files a user uploads to the gallery is viewable by all logged in users. Since these files may be of a delicate nature this will not work. I need them to be hidden from everyone but the user that uploaded them.

If I had to put togeather a wish list These would be my top two: Add a file upload easy control then a check box to hide user generated/uploaded content from everyone except the user that creats that content. This is already (sorta) implimented where you can hide certain menus from logged out users in the administration menu creation section so it shouldn't be too difficult to add to other controls with a user name check to see which content they should see.

I haven't had the chance to dig very deep into the code for MWPSK yet but from everything I've read in these discussions it looks like a great start for someone that doesn't want to go through the rigors of entire site developement.

I'm new to MWPSK but I like what I've seen so far and can't wait to dig deeper into it.
Great job!
Nov 30, 2007 at 7:41 AM
Edited Nov 30, 2007 at 7:42 AM


vss wrote:
Does someone manage a solution for roles and visibility in menu both working toghether.



I added a variable VisibleInMenu, implemented a rolemanager that handles all pages that works fine.
When an unautencticated user visiting the website, he/she can't see the links that are in roles.
When the user are logged in, the menu updates and the links that this user has right to see, becomes visible.
Nov 30, 2007 at 5:49 PM
Edited Nov 30, 2007 at 5:51 PM
I have already implimented this to make visable a logged in users link on the menu, called "My Account". The problem I have is that each of my users will need different content displayed within that link. There are also two sub pages in that link, one to submit content to their "locker" and another to retreave that content. Each users content will be ONLY what THAT user submits to their "locker" and UNACCESSABLE to ANY other logged in users.

I believe this is what vss is looking for also except more twards just the menu options.
Dec 2, 2007 at 9:29 AM
Hi again
It looks like it's the section in that page that has to have a special userselector in it.
Couldn't you get the userid that logs in and then set the content for each person.

If you build "real" pages and add that functionallity to it this should't be a problem. It's not a problem to link this pages in sitemap.
Dec 3, 2007 at 4:25 AM


It looks like it's the section in that page that has to have a special userselector in it.
Couldn't you get the userid that logs in and then set the content for each person.


That is what I have been trying to do. I can't figure out how to extract the logged in users userID from the Users.Config file. Looking at the Users.Config file it looks like PKID is what I'm trying to get.

NOTE: I'll be using curley brackets in place of solid brackets or the wiki markup will make them links.

I'm wanting to set the upload path of the file to: ~/userImages/{LogedUserId}/somefilenam.xxx

Where {LoggedUserId} is the user id from the Users.Config file. This way each of my users will have their own folder where uploaded files will be kept. I think this will be easier to display that content when I need to get it later. Here is what I have so far.

13 public partial class EasyControls_fileUpload : System.Web.UI.UserControl
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 int UserID = Convert.ToInt32(SessionLoggedUserId);
18 if (EditFileUpload.HasFile)
19 {
20 byte{} bytes = EditFileUpload.FileBytes;
21 System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
22 System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
23 string ImageName = EditFileUpload.FileName;
24 string dipath = System.Web.HttpContext.Current.Server.MapPath("") + "\\userImages\\" + SessionLoggedUserId.ToString();
25 string imagePath = "~/userImages/" + SessionLoggedUserId.ToString() + "/" + EditFileUpload.FileName;
26 DirectoryInfo di = new DirectoryInfo("" + dipath + "");
27 if (!(di.Exists))
28 {
29 di.Create();
30 }
31 string serverPath = dipath + "\\" + ImageName;
32 img.Save(serverPath);
33 }
34 }
35 }
Dec 3, 2007 at 8:12 AM
Edited Dec 3, 2007 at 8:30 AM
Hi again
An easy way of doing this, is to create a folder in App_Data that is called Users. In that folder you create a folder with the username for each user.
To get the logged in users name, use: User.Identity.Name;
In this application its safe to use the name since it's unique for each user, otherwise it's better to use the PKID.

//protected FileUpload EditFileUpload;
protected void AddUserImage()
{
if (User.Identity.IsAuthenticated)
{
string username = User.Identity.Name;
if (EditFileUpload.HasFile)
{
byte[] bytes = EditFileUpload.FileBytes;
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
string ImageName = EditFileUpload.FileName;
string dipath = System.Web.HttpContext.Current.Server.MapPath("") + "\\Users\\" + username;
string imagePath = "~/Users/" + username + "/" + EditFileUpload.FileName;
DirectoryInfo di = new DirectoryInfo("" + dipath + "");
if (!(di.Exists))
{
di.Create();
}
string serverPath = dipath + "\\" + ImageName;
img.Save(serverPath);
}
}
}

You also have to add a Imagehandler to view images. You use your path as:
string username = User.Identity.Name;
string path = context.Server.MapPath(string.Format("~/App_Data/{0}/{1}", username,file));


Another way of doing this is to create a Section that has your specific needs. That's an easy part and you can add this functionality to the control.
Dec 24, 2007 at 10:21 PM
I also want to know if there is a new solution for the VisibleInMenu Attribute for SubLevel menu.
The solution that was posted here is not full, and I wasn't able to fix it (some issues regarding to Collection is read only).
Have anyone solved this?
Dec 26, 2007 at 9:20 AM


eyalgursoy wrote:
I also want to know if there is a new solution for the VisibleInMenu Attribute for SubLevel menu.
The solution that was posted here is not full, and I wasn't able to fix it (some issues regarding to Collection is read only).
Have anyone solved this?


If you really need this option you can change this in CustomXmlSitemapProvider.cs :
public override bool IsAccessibleToUser(HttpContext context, SiteMapNode node)
At the bottom you change "return ((page.Visible) && (page.AllowAnonymousAccess))"

with:

bool visibleinmenu=false;
if ( page.VisibleInMenu != null )
{
if(page.VisibleInMenu == true)
visibleinmenu = true;
}

return ((page.Visible) && (page.AllowAnonymousAccess) && (visibleinmenu));
Dec 26, 2007 at 12:10 PM

rickardmagnusson wrote:


eyalgursoy wrote:
I also want to know if there is a new solution for the VisibleInMenu Attribute for SubLevel menu.
The solution that was posted here is not full, and I wasn't able to fix it (some issues regarding to Collection is read only).
Have anyone solved this?


If you really need this option you can change this in CustomXmlSitemapProvider.cs :
public override bool IsAccessibleToUser(HttpContext context, SiteMapNode node)
At the bottom you change "return ((page.Visible) && (page.AllowAnonymousAccess))"

with:

bool visibleinmenu=false;
if ( page.VisibleInMenu != null )
{
if(page.VisibleInMenu == true)
visibleinmenu = true;
}

return ((page.Visible) && (page.AllowAnonymousAccess) && (visibleinmenu));


Thanks,
I'll give it a try.
Dec 26, 2007 at 2:48 PM
Hi rickardmagnusson,

I've checked the solution you suggested, it is working just fine for the menu. But, what if I want to link to a page that is invisible in the menu from another page (let's say from HTML module)?
The access to that page is now blocked :(
I've tried the solution from one of the posts here
http://www.codeplex.com/MyWebPagesStarterKit/WorkItem/View.aspx?WorkItemId=11268
But that solution only hides the highest level nodes from the menu.
I've tried fixing the code over there to work recursively, but it didn't work...
I even thought about holding two Sitemap files, one for the menu, and one for the users to access. But I'm not even sure if it's possible.

Is there a way to access a page which is not on the menu?
Dec 27, 2007 at 9:18 AM
Edited Dec 27, 2007 at 1:29 PM


eyalgursoy wrote:
Hi rickardmagnusson,

I've checked the solution you suggested, it is working just fine for the menu. But, what if I want to link to a page that is invisible in the menu from another page (let's say from HTML module)?
The access to that page is now blocked :(
I've tried the solution from one of the posts here
http://www.codeplex.com/MyWebPagesStarterKit/WorkItem/View.aspx?WorkItemId=11268
But that solution only hides the highest level nodes from the menu.
I've tried fixing the code over there to work recursively, but it didn't work...
I even thought about holding two Sitemap files, one for the menu, and one for the users to access. But I'm not even sure if it's possible.

Is there a way to access a page which is not on the menu?



Hi again
Sorry, the IsAccessibleToUser has nothing to do with menuitems visibillity. I post a new solution for this.

Create a new UserControl, add a new Menu
<asp:Menu ID="Menu1" runat="server" DataSourceID="menuDataSource" OnMenuItemDataBound="Menu1_MenuItemDataBound">
</asp:Menu>
<asp:SiteMapDataSource ID="menuDataSource" runat="server" StartingNodeOffset="1" ShowStartingNode="false" />

In codefile add:

protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
{
if (!Page.User.IsInRole("Administrators"))
{
Menu menu = (Menu)sender;
SiteMapNode Node = (SiteMapNode)e.Item.DataItem;

if (Node"VisibleInMenu" != null)
{
if (Node"VisibleInMenu" == "False")
{
menu.Items.Remove(e.Item);
}
}
}
}

You also have to remove these lines in CustomSiteMapDataSource in RecursiveNodesCheck
if (Node.ChildNodes.Count == 0){
}
Dec 27, 2007 at 4:36 PM
Edited Dec 27, 2007 at 4:39 PM
First of all, thanks allot man!

I've tried your solution, unfortunately it didn't work...
I've added the new user control to my Skin file (I was supposed to do that, right?) but now I had no menu.

The RecursiveNodesCheck function name made the difference for me, as I found out I wasn't using the latest version of the CustomSiteMapDataSource class. So I looked up a little bit more and found my request.
Every thing is working just fine now :)
I don't understand why the new version is not released in the Download page...

Anyway, thanks again