Microsoft SharePoint 2007 is a little bit complicated with permissions. Roles are one important part about it. Roles define user's permission to the site or files. MS SharePoint contains five permission level by default(Full Control, Design, Contribute, Read, Limited Access). Also you can create your own groups and set them their permission level. I will not describe Microsoft SharePoint permission logic here because you can easily google it and you would found a lot of useful sources.
Here is the some for the start:
http://office.microsoft.com/en-us/windows-sharepoint-services-help/permission-levels-and-permissions-HA010100149.aspx
http://msdn.microsoft.com/en-us/library/ms252445(v=vs.80).aspx
http://office.microsoft.com/en-us/sharepoint-server-help/about-managing-sharepoint-groups-and-users-HA010021579.aspx
Let's imagine hypothetical project. You should create webPart which would view some data (data source is SP list somewhere) and that data should be editable for users who has at least Contribute permission level to the site. So whenever you render webPart you need to check user's permission level at first. This code should help you to check if the permission level of current user is greater or equal to another permission level.
bool isCurrentUserRolesGreaterOrEqualThan(string role)
{
//User's permission level
bool limitedAccess = false;
bool read = false;
bool contribute = false;
bool design = false;
bool fullControl = false;
//Current web
SPWeb web = SPContext.Current.Web;
//Gets all user's roles
SPRoleDefinitionBindingCollection usersRoles = web.AllRolesForCurrentUser;
//Role definitions to compare with user's roles
SPRoleDefinitionCollection roleDefinitions = web.RoleDefinitions;
//Checks user's permission level-Read, Contribute, Design, Full Control, and Limited Access
if (usersRoles.Contains(roleDefinitions["Read"]))
read = true;
if (usersRoles.Contains(roleDefinitions["Contribute"]))
contribute = true;
if (usersRoles.Contains(roleDefinitions["Design"]))
design = true;
if (usersRoles.Contains(roleDefinitions["Full Control"]))
fullControl = true;
if (usersRoles.Contains(roleDefinitions["Limited Access"]))
limitedAccess = true;
//Compares given role with user's permission level
if (role == "Limited Access")
if (limitedAccess || read || contribute || design || fullControl)
return true;
if(role == "Read")
if (read || contribute || design || fullControl)
return true;
if(role == "Contribute")
if (contribute || design || fullControl)
return true;
if(role == "Design")
if (design || fullControl)
return true;
if(role == "Full Control")
if (fullControl)
return true;
SPUser currentUser = web.CurrentUser;
if (currentUser.IsSiteAdmin)
{
return true;
}
return false;
}
OK. And that's all :) I hope it could help somebody...
Happy coding
David
What if user has a custom Permission level?
ReplyDeleteHi,
ReplyDeleteIt depends on what you want to achieve. I would prefer to use default role definitions if possible, because than you can follow rights hierarchy. That means that you are able to distinguish between less evaluated and more evaluated user. In a case custom permission level you loose this advantage. Probably you are trying to create some logic such as "Show SPList item with blue color if user can delete list item". In that case you have to search for base permission.
Here is the link(MSDN): http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.sproledefinition.basepermissions.aspx
and here is example: http://sharepointmalaya.blogspot.cz/2009/07/validate-user-base-permissions-before.html
I hope I understood you properly and this information will help you.