Showing posts with label Permissions. Show all posts
Showing posts with label Permissions. Show all posts

Tuesday, 9 December 2014

SharePoint Security 101: What You Need to Know to Secure SharePoint

For most organizations, a healthy security stance is essential to protecting and preserving Microsoft SharePoint content. Depending on your industry, you might even be bound by regulations that require you to secure and audit access to some content.
Certainly, security is a broad topic that involves not only SharePoint farm administrators, but server, network, and database admins as well. Through delegation to site collection admins and site owners in the business units, security also involves day-to-day end users.
To effectively enforce business-driven information-management policies, collaboration with the business is also necessary.
In response to the vast number of interested security stakeholders, SharePoint 2010 offers a flexible security model that supports multiple authentication types and multiple levels of permission authorization.
But this flexibility has a downside: Without a clear, published strategy for how permission management should be performed in your organization, security chaos can make day-to-day usage quite painful.
With all these variables, it's no wonder that security can become a complex equation.
Rest assured that armed with a clear understanding of your security requirements, some technical guidance, and bit of common sense, you can build a security model that is both effective and sustainable.
In this three-part series, we aim to provide you with this technical guidance -- and we'll throw in some best practices for good measure.
In this article, I'll start off with the basics, provide you with several step-by-step procedures, and make sure that you have a good overall understanding of the SharePoint authorization model. The focus will be on the farm and work through web applications and site collections, down to individual sites, lists, libraries, and items. In Part 2, Kevin Laahs will focus on claims-based authentication; in Part 3, Todd Klindt will talk about hardening your SharePoint servers.

SharePoint's Logical Hierarchy

Before we get into setting permissions, let's review the basic terminology of a SharePoint environment. A SharePoint farm refers to all the servers (i.e., web front end, application servers, and database servers) that work together to provide a SharePoint service to users. Within the farm, a structured hierarchy organizes content, as Figure 1 shows. The purpose of this hierarchy is to organize and secure the vast amount of content within SharePoint.
 
Figure 1: SharePoints logical hierarchy

Figure 1: SharePoint's logical hierarchy

Securing the Farm

Despite the enormous responsibility that it implicates, securing the farm is quite a simple process. Permissions are mostly cut and dried: You're either a farm admin, or you aren't. A farm admin has full control over the farm, including managing web applications, starting and stopping services, backing up or restoring the farm -- effectively, any task that can be found in Central Administration, SharePoint's web-based administration interface. A farm administrator can also grant anyone access to any and all content within SharePoint. Indeed, the farm admin role is a powerful one, and you should be cautious about who is granted this level of access.
Farm administrator access is granted by adding Active Directory (AD) users or security groups to the farm administrators group within Central Administration:
1. Within Central Administration, click Security and then select Manage The Farm Administrators Group.
2. Add or remove AD users or groups as needed.
Security note: To perform certain administrative tasks such as creating web applications, you also need to be a local Windows administrator on the web server (or servers) that run the Central Administration web application.

Granting PowerShell Access

Although Central Administration is commonly used for many day-to-day operations, Windows PowerShell is an incredibly powerful way to administer and automate SharePoint functions from a command-line interface. (To learn more about using PowerShell with SharePoint, see " Using Windows PowerShell to Manage SharePoint 2010 " and " SharePoint Administrators Can Learn to Love PowerShell .")
To grant permissions for others to run PowerShell commands, use the Add-SPShellAdmin cmdlet. This command grants necessary permissions on SharePoint servers, the configuration database, and (optionally) a SharePoint content database. If you run this command without specifying a database, then the user is granted access to the farm only, not to any content databases. To grant a user the ability to run all cmdlets within a content database (e.g., to use Get-SPWeb to return a site), use the -database switch to grant the user access to the content database.
Security note: To use the Add-SPShellAdmin cmdlet to grant PowerShell access, you must be a farm administrator, a local administrator on the server on which the command is run, and a security admin on the database server (or servers) to which you are granting permission.

Securing a Web Application

A web application is a user's entry point into SharePoint. Specifically, a web application consists of one or more Microsoft Internet Information Server (IIS) websites that control how users are authenticated (or not authenticated, if you allow anonymous access). Organizations commonly have multiple web applications to isolate certain types of content or sets of users, such as a Record Center or extranet, respectively. When granting permissions to a web application, the sole purpose is to control who has access to the content within the site collections that are associated with the web application. In other words, you cannot grant permissions to administer a web application; only farm admins can do that, and they have administrative control over all web applications.
To adjust permissions to content within a web application, change the user policy for the web application. You can make this change in Central Administration:
1. Within Central Administration, click Application Management.
2. Select Manage Web Applications.
3. Highlight the web application and click the User Policy button in the Ribbon.
4. Add or deny permissions, as needed.
Security Note: Because permissions can also be set for individual site collections (which I cover in the next section), permissions aren't usually granted this way. This method is very powerful and is used for special purposes. For example, you might decide to use it to grant read-only access to auditors, to ensure that they have access to all site collections in the web application. Furthermore, this web application policy is the only method for denying someone access to SharePoint. This setting takes precedence over any other permission that is granted. Also, none of the permissions that you grant here are visible at a lower level. For example, a site collection administrator can't see who has been granted permissions through the web application policy. Again, this method of setting permissions is powerful but potentially confusing and dangerous -- so use it with caution!

Allowing Anonymous Access

Anonymous access allows non-authenticated (i.e., guest) users to access a SharePoint web application. Anonymous access is commonly used for Internet-facing sites but can also be used for intranets. Anonymous access can be enabled whether you use claims or classic authentication. Here's how you enable anonymous access:
1. Within Central Administration, click Application Management.
2. Select Manage Web Applications.
3. Highlight the web application and click the Authentication Providers button in the Ribbon.
4. Select the zone. (A zone allows you to expose a web application with a unique URL. You can have as many as five zones per web application. All zones have access to the content databases and site collections that are associated with that web application.)
5. Select the Enable Anonymous Access check box and click Save.
Security note: By default, anonymous users have no access to site collections until that permission is granted. Therefore, after anonymous access has been enabled, you must set which permissions anonymous users have. (I cover this topic in the Grant Anonymous Access section of this article.)

Securing a Site Collection

Although you can grant permissions to multiple site collections through the web application policy, save this method for special circumstances. Users are authenticated at the web application level and authorization is typically performed at the site collection level. In other words, for regular, day-to-day permission management, you should assign permissions via site collections.
By default, permissions are inherited or cascade down to all websites, lists, libraries, folders, and items in the site collection hierarchy. Thus, permissions that are granted to the top-level site also apply to a document buried deep within a site collection. As you'll learn, this inheritance can be stopped.
The highest level of access within a site collection belongs to the site collection administrator. This user has implicit access to all SharePoint content (e.g., sites, lists, libraries, items) within the site collection, whether or not permission is specifically granted. Through Central Administration, a farm admin can specify up to two site collection administrators for each site collection. Within each site collection, however, a site collection administrator can add additional admins as needed.
You grant regular users (i.e., non-administrators) access through the site collection. In fact, each site collection is administered separately, making these collections very convenient administrative units that can be delegated out. Because administration occurs from within the site collection (and not Central Administration), administrative tasks are often assigned to trained business users, empowering them to be somewhat self-supporting. Of course, depending on your site collection architecture, this administrative boundary can become a challenge. For example, if you need to grant access to multiple site collections, that access must be granted one collection at a time for each user or group of users.
Security Note: If a user tries to access a site to which they don't have permissions, of course they get an access denied error. But you can have the user request access from this error page. To learn more, see " How to Manage SharePoint Site Access Requests."

Understanding SharePoint Groups

Like many other systems, SharePoint uses the notion of groups to simplify the assignment of permissions to multiple users. Note that SharePoint groups are specific to a site collection. Thus, you cannot use one SharePoint group to span multiple site collections. Again, this can be a blessing (when you want administrative separation) or a curse (when you don't). Also, SharePoint groups can't be nested, meaning that you can't place one SharePoint group within another. Some of these limitations don't apply to AD security groups, however. For example, you can assign one AD group permissions to multiple site collections. To better understand your options, here are the different ways permissions within a site collection can be granted:
  • Add one or more users to a SharePoint group and grant that group permissions.
  • Add one or more users to an AD security group and grant that group permissions.
  • Add one or more users to an AD security group, add the AD group to a SharePoint group (this type of nesting is allowed), and grant the SharePoint group permissions.
  • Grant permissions directly to one or more users, without using groups.
You assign permissions to users or groups by granting permission levels. Each site collection has its own built-in set of permission levels; you might be familiar with some of them (e.g., Full Control, Design, Contribute, Read). You can also create custom permission levels for more granular control over the granted access. Here are the steps to grant permissions within a site collection:
1. Within the site collection, go to the Site Actions menu and select Site Permissions.
2. On the Ribbon, click the Grant Permissions button.
3. In the dialog box, select individual users, SharePoint groups, or AD security groups.
4. Specify whether you want to add these users to a SharePoint group or to grant permissions directly. (Note: If you selected one or more SharePoint groups in step 3, you can only grant permissions directly.)
5. Optionally, send these users a welcome email.
When creating a new SharePoint group, you can also assign permissions to the group. Doing so makes permission assignment easy because you need only add users into the group. You can create new SharePoint groups from the same Site Permission screen on which you grant permissions.
Security note: When creating a new site collection, three default SharePoint groups are created automatically: owners (who have Full Control access), members (who have Contribute access), and visitors (who have Read-Only access).

Permission Inheritance

By default, permissions that are set for the top-level site within a site collection apply to all content in the site collection hierarchy (as Figure 1 shows). This concept is called permission inheritance and it helps to simplify permission management.
You can no doubt think of cases in which you need to establish unique permissions for a certain level. For example, you might want to protect a subsite and ensure that only managers have access. Within SharePoint, you can stop this permission inheritance on four different objects within the site collection: sites, lists/libraries, folders, and items. When you stop inheriting permissions, you can establish a unique ACL for that object, which then cascades to lower levels. This gives you a great deal of flexibility to mold a specific set of permissions around your content. However, be careful: The more you stop inheritance, the more complex permission management becomes over time. Another troublesome side effect of stopping inheritance too often is that it can drastically affect SharePoint's performance. As a general rule, stopping inheritance should be the exception, and you are encouraged to structure your sites, libraries, and folders accordingly. Figure 2 depicts an example of permission inheritance.

Figure 2: Permission inheritance in action

Figure 2: Permission inheritance in action  The basic steps to stop inheritance and assign permissions are the same, regardless of the object type. For a site, use the Site Permissions link in the Site Actions menu. For a list or library, use the list's or library's settings page. For a folder or item, use the object's context menu, sometimes called the edit control block. If inheritance is stopped on an object, you can revert back to inherit, but any custom permissions that you applied will be lost.
Security note: Despite your best efforts, permission management can still spiral out of control. Another way to alleviate this is to have a good governance plan that gives administrative end users clear guidance about how to use groups and when or if to stop inheritance. Another option is to use third-party software such as AvePoint's DocAve Administrator, which can help manage permissions across the whole farm in one operation. Tools like this also help enforce a security policy as defined in a governance plan.

Granting Anonymous Access

If the web application is configured to allow anonymous access, you can adjust which permissions an anonymous user receives:
1. Access the permissions page for the object, as described in the previous section.
2. If necessary, click the Stop Inheriting Permissions button in the Ribbon. (You can't adjust anonymous access on an object that inherits permissions from its parent.)
3. In the Ribbon, click the Anonymous Access button.
4. Choose the permissions to grant to anonymous users. The available permissions will vary depending on the type of object.
5. Click OK to save.

Wrapping Up

Hopefully, this security overview wasn't too painful and served as a useful refresher for those who have a SharePoint background. By now, you should understand SharePoint's authorization capabilities and some of its limitations. You've learned how to define farm and PowerShell administrators, adjust a web application's user policy, and configure each of the four permissionable objects (i.e., site, list/library, folder, and item) within a site collection. I've also given you some quick steps to enable anonymous access and set anonymous permissions. In the next article of this series, Kevin Laahs will focus on authentication, specifically the claims-based authentication support in SharePoint 2010. Until then, be safe and secure.

Reference:

http://sharepointpromag.com/sharepoint-2010/sharepoint-security-101-what-you-need-know-secure-sharepoint

Permissions in SharePoint 2010

Farm Administrators

Farm Administrators group is a group that is managed centrally via SharePoint Central Administration web-site:

Farm Administrators include by default SharePoint Farm -account, SharePoint installation account and BUILTIN\Administrators group. Farm Administrators have basically “all rights” in SharePoint Farm.
You can give Farm Administration rights to groups and users:


Authentication Providers

With authentication providers you can control how you would like to have your users authenticated in a web application. You can also enable/disable anonymous access:

Web Application Level Permission Policies

With web application level permission policies you can control centrally, with Central Administration, what kind of permission policies you want to apply to all site collections and sites under specific web application. SharePoint gives us four predefined policies by default:


Web Application Level User Policies

User Policy is the place where the magic happens in a web application level. User policy is basically a AD user or AD group mapping to certain Web Application Level Permission policy. You can even define a Zone in which the policy is applied. For example you can use different policy for users who use the SharePoint sites from your internal network (intranet zone), and different policy for those who access the sites through public internet (internet zone), or just apply to “All Zones”.

Web Application Level Anonymous Policy

You can also define web application level anonymous users’ policy through Central Administration -site (you can only select the policy from a three predefined policies):

Web Application Level User Permissions

This is just a checkbox list from where you can manage what kind of permission levels can be used in a web application and site collections, you can add/reduce certain permission for the whole web application:

Site Collection Administrators

Site Collection Administrators have full control of a specific SharePoint site collection. You can only use AD users as site collection administrators. With Central Administration site, you can define two users as site collection administrators, but in site collection settings you can add more site collection administrators. Here is a screenshot of Central Administration site collection administrators settings page:


Anonymous Access Permissions

You can control what parts of your site the Anonymous users can access with Anonymous Access Setting:

Anonymous access can further be restricted by enabling View Form Pages Lock Down -feature. Our advice is to enable this feature in every public SharePoint site.
If anonymous access is enabled, you can control the permission of anonymous in list or library:

Site Collection Level Permission Levels

Like in Web Application level permission policies, these are the actual permissions that SharePoint will check when user accesses resources in a SharePoint site. This time we have Grant only abilities (in Web Application Level Permission Policies you could use Grant and Deny). In itself permission levels are only definitions that group the more fine grained permissions together in a more useful entity.
SharePoint has these permission levels defined in site collections:

You can also define your own permission levels, if predefined levels do not match the requirements. Own permission levels can be created in similar fashion as web application level permission policies:

SharePoint Groups

SharePoint groups are a little bit like AD groups, but these groups are managed in SharePoint instead of AD. SharePoint groups can be used to delegate rights management for the site owners instead of system administrators. SharePoint groups are global to the whole site collection. You cannot specify SharePoint group that exists only in a site level. SharePoint groups cannot be used over the site collections. One thing SharePoint groups do support that AD groups do not, is membership requests. You can control SharePoint groups’ permission levels whenever you want to use that group. Basically SharePoint group is just a collection of AD groups and AD users with attached permission levels. While permission level can change for the group the members are globally defined.
Here is a small clipping of Group creation settings:

SharePoint Groups do not directly give any rights to AD users or AD groups. You have to use that group somewhere. Next we walk through all the places where you can use SharePoint Groups, AD Groups and AD users to actually give the permissions.

Site Permissions



Site permissions are where all the permission management begins. More specifically the root site permissions. These are the permissions that all sub-items (except column permission)will inherit. That’s why it is important to carefully design the site permissions as the whole site will use these by default unless the inheritance chain is broken.
When you grant site permissions you can use AD groups, AD users and SharePoint groups. You can either add users to some of SharePoint groups or grant the permissions directly.
Here is a screenshot of SharePoint site level permission granting screen (this exact same functionality is also used in other levels described below):

Each sub site can break the permissions inheritance chain and specify their own permissions, just like you specify them in a root site.

Library or List Permissions



Library and List permissions can be managed though list settings. Basically the management works exactly the same as with Site permissions. First you break the inheritance chain and then you start to manage individual list’s or library’s permissions. You can grant rights for AD users, AD groups and SharePoint Groups. By default libraries and lists inherit their permissions from parent site.
With lists and libraries you have also some other security related features.
For example you can control Draft Item Security:


Folder, Document or Item Permissions

Like with library and site permissions, folders, documents or items can be granted with their own permissions by breaking the permissions inheritance chain.
You can access document and item level permission settings page directly from the object you are interested in:

Column Permissions

In SharePoint, this level of permissions does not exist. But the new permission control for SharePoint has been provided by BoostSolutions, so you can assign different permissions to different users for different columns.
After install Column Permission product on SharePoint, you can access column permission settings page in List Settings page to assign the different permissions for columns.


Reference:

http://www.boostsolutions.com/blog/permissions-in-sharepoint-2010/
 


Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in SharePoint 2010.
Question: What is Permission level?
 
In simple terms "The permission levels are set for the permissions defined that grants permission to users or groups to perform specific actions. The default permission levels are as follows: Read, Contribute, Design, and Full Control".
Step 1: Open SharePoint 2010 Central Administration and navigate to a specific site.

Step 2: Open up Visual Studio 2012 and try to "SharePoint Visual Web Part" project, as in:

open-sharepoint2010-visual-webpart.jpg
 
Step 3: Select "Deploy as a farm solution" as in the following and click the "Finish" button.

Sharepoint-customization-wizard.jpg
 
Step 4: The complete code of visualwebpart1usercontrol.ascx looks like this:
 
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@  Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1.ascx.cs"
    Inherits="AddPermissionLevelApp.VisualWebPart1.VisualWebPart1" %>
<%--<center>--%>
<div>
    <table style="font-family: Verdana; text-align: center;">
        <tr>
            <td colspan="2">
                <asp:label id="Label1" runat="server" text="Add Permission Level - SharePoint 2010 via Visual Studio 2012"
                    font-bold="true" forecolor="Maroon" font-size="Large"></asp:label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:label id="Label2" runat="server" text="Please Enter Permission Level Name:"></asp:label>
            </td>
            <td>
                <asp:textbox id="TextBox1" runat="server" width="117px"></asp:textbox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:label id="Label3" runat="server" text="Please Enter Permission Level Description:"></asp:label>
            </td>
            <td>
                <asp:textbox id="TextBox2" runat="server" textmode="MultiLine" width="117px"></asp:textbox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:button id="Button1" runat="server" text="Add Permission Level" forecolor="Orange"
                    font-bold="true" backcolor="Black" onclick="Button1_Click" width="228px" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:label id="Label4" runat="server" font-bold="true"></asp:label>
            </td>
        </tr>
    </table>
</div>
<%--</center>--%>

Step 5: The complete code of visualwebpart1usercontrol.ascx.cs looks like this:
 
using Microsoft.SharePoint;
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;namespace AddPermissionLevelApp.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public partial class VisualWebPart1 : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling using
        // the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public VisualWebPart1()
        {
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Focus();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))
            {
                Label4.Text = "Please Enter Some Values";
                Label4.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                SPWeb web = SPContext.Current.Web;
                SPRoleDefinition rDefination = new SPRoleDefinition();
                rDefination.Name = TextBox1.Text;
                rDefination.Description = TextBox2.Text;
                rDefination.BasePermissions = SPBasePermissions.ManageAlerts |SPBasePermissions.AddListItems | SPBasePermissions.EditListItems | SPBasePermissions.DeleteListItems;
                web.RoleDefinitions.Add(rDefination);
                web.Update();
                Label4.Text = TextBox1.Text + " - Permission Level Created Successfully";
                Label4.ForeColor = System.Drawing.Color.Green;
                TextBox1.Text = string.Empty;
                TextBox2.Text = string.Empty;
            }
        }
    }
}

Step 6: Deploy the solution file and add the new webpart to a SharePoint site.

Step 7: The output of the application looks like this:

add-permission-level-sharepoint2010.jpg
 

Step 8: Data entering output of the application looks like this:

Data-entering-output -sharepoint2010.jpg
 


Step 9: Permission level created output of the application looks like this:

Permission-level-created-output-sharepoint2010.jpg
 
I hope this article is useful for you.



Reference:

http://www.c-sharpcorner.com/UploadFile/54db21/create-permission-level-in-sharepoint-2010-programmatically/

Programmatically add or remove item level permission in Sharepoint 2007


In this article I will show you how to Add or Remove Item Level Permissions programmatically. I will show you this thing in console application. You can use it anywhere based upon your requirements. 

Sometimes we are having a business requirement like if someone is adding any item to a list or document library than some users should have read permissions to it, some of them should have write permissions and some of the users should not have any access to that particular list item.

Then how to achieve this kind of requirements.

In ideal situations we are writing Item Adding event handlers and based upon business rules we are giving the access and removing the access because whenever any user is adding any item than that item will inherit the site level permissions. So on this event we can add or remove the permissions to that list item.

Let's see how we can add or remove permissions at item level.

Step 0: Create one list named Check and add 2-3 items in it.

1.gif
 
Click on Manage Permissions link and see who all are having permissions to that particular list item.

2.gif
 
In my case on "Ravish" list item this much user or Groups are having access. Now our task is to remove all the users or groups who are having the access and add only administrator to it. i.e. only administrator should have the access.

We are doing this through sharepoint object model. Below are the steps of doing this.

Step 1: Create one console application.

3.gif 

Step 2: Add refernce to Microsoft.Sharepoint.dll

4.gif
 
Step 3: Navigate to the List item by using sharepoint object model.

//Connect to Sharepoint Site
SPSite oSPSite = new SPSite("http://spdevserver:1002/");
//Open Sharepoint Site
SPWeb oSPWeb = oSPSite.OpenWeb();
//get the Sharepoint List
SPList oSPList = oSPWeb.Lists["Check"];
//Get the Sharepoint list item for giving permission
SPListItem oSPListItem = oSPList.Items[0];
Console.WriteLine(oSPListItem["Title"]);
Console.Read();

5.gif
 
Step 4: Now we have navigated to List Item Ravish. Next step is to remove all the permissions for this list item.

Function Call

RemoveAllPermissions(oSPListItem);  

Function Definition:

private static void RemoveAllPermissions(SPListItem CurrentlistItem)
{
    //The below function Breaks the role assignment inheritance for the list and gives the current list its own copy of the role assignments
    CurrentlistItem.BreakRoleInheritance(true);
    //Get the list of Role Assignments to list item and remove one by one.
    SPRoleAssignmentCollection SPRoleAssColn = CurrentlistItem.RoleAssignments;
    for (int i = SPRoleAssColn.Count - 1; i >=0 ; i--)
    {
        SPRoleAssColn.Remove(i);
    }
    Console.WriteLine("All Permissions Removed");
}

6.gif

After this check List Item permissions once agin by clicking on Manage Permissions Link.

7.gif
  
You will see there are no items to show in this view. Means all the permissions for this list item has been removed.

Step 5: Next step is to add permissions to the same list item.

//Create new user to grant access
SPUserCollection users = oSPWeb.Users;
SPUser CurrentUser = users["Domain\\Administrator"];
//Add new permissions to List Items
//If you want to give access to a Group than pass SPGroup instead of SPUser. The same function will give access.
GrantPermission(oSPListItem, oSPWeb, SPRoleType.Contributor, CurrentUser);

Function Definition:

private static void GrantPermission(SPListItem CurrentListItem, SPWeb oSPWeb, SPRoleType SPRoleType, SPPrincipal SPPrincipal)
{
    //Create one Role Definition i.e Full Controls, Contribute rights or Read rights etc.
    SPRoleDefinition oSPRoleDefinition = oSPWeb.RoleDefinitions.GetByType(SPRoleType);
    //Create one Role Assignment for the specified SP user or group.     SPRoleAssignment oSPRoleAssignment = new SPRoleAssignment(SPPrincipal);
     //Bind the role definition to the role assignment object created for the user or group.     oSPRoleAssignment.RoleDefinitionBindings.Add(oSPRoleDefinition);
     //Add it to the specified list item.     CurrentListItem.RoleAssignments.Add(oSPRoleAssignment);
     //update the list item so that specified user assignment will have the access.     CurrentListItem.Update();
     Console.WriteLine("All Permissions Removed");
     Console.Read();

}

8.gif

After this check List Item permissions once again by clicking on Manage Permissions Link.

9.gif
 
You will see that new user has been added to the particular list item.

10.gif
 
In this way we can add or remove the permission at Item Level in a sharepoint site.

Hope it will be a help to you.


Reference:

http://www.c-sharpcorner.com/uploadfile/Ravish001/programmatically-add-or-remove-item-level-permission-in-sharepoint-2007/

Delete Permission Level in SharePoint 2010 Programmatically Using VS 2012

Introduction
Today, in this article let's play around with one of the interesting and most useful concepts in SharePoint 2010.
Question: What is Permission level?
 
In simple terms "The permission levels are a set of permissions defined that grants permission to users or groups to perform specific actions. The default permission levels are as follows: Read, Contribute, Design, and Full Control".

Step 1: Open SharePoint 2010 central administration and navigate to a specific site.

Step 2: Open up Visual Studio 2012 and try to "SharePoint Visual Web Part" project, as in:

open-sharepoint2010-visual-webpart.jpg
 
 Step 3: Select "Deploy as a farm solution" as in the following and click the "Finish" button.

sharepoint-customization-wizard.jpg

Step 4: The complete code of visualwebpart1usercontrol.ascx looks like this:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@  Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1.ascx.cs"
    Inherits="DeletePermissionLevelApp.VisualWebPart1.VisualWebPart1" %>
<div>
    <table style="font-family: Verdana; text-align: center;">
        <tr>
            <td colspan="2">
                <asp:label id="Label1" runat="server" text="Delete Permission Level - SharePoint 2010 via Visual Studio 2012"
                    font-bold="true" forecolor="Maroon" font-size="Large"></asp:label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:label id="Label2" runat="server" text="Please Enter Permission Level Name:"></asp:label>
            </td>
            <td>
                <asp:textbox id="TextBox1" runat="server" width="117px"></asp:textbox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:button id="Button1" runat="server" text="Delete Permission Level" forecolor="Orange"
                    font-bold="true" backcolor="Black" onclick="Button1_Click" width="228px" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:label id="Label4" runat="server" font-bold="true"></asp:label>
            </td>
        </tr>
    </table>
</div>

Step 5: The complete code of visualwebpart1usercontrol.ascx.cs looks like this:
 
using Microsoft.SharePoint;
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;namespace DeletePermissionLevelApp.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public partial class VisualWebPart1 : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling using
        // the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public VisualWebPart1()
        {
        }
        protected override void OnInit(EventArgs e){base.OnInit(e);
            InitializeControl();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Focus();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TextBox1.Text))
            {
                Label4.Text = "Please Enter Some Values";
                Label4.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                SPWeb web = SPContext.Current.Web;
                SPRoleDefinition rDefination = new SPRoleDefinition();
                web.RoleDefinitions.Delete(TextBox1.Text);
                web.Update();
                Label4.Text = TextBox1.Text + " - Permission Level Deleted Successfully";
                Label4.ForeColor = System.Drawing.Color.Green;
                TextBox1.Text = string.Empty;
            }
        }
    }
}
Step 6: Deploy the solution file and add the new webpart to a SharePoint site.
Step 7: The output of the application looks like this:

delete-permission-level-sharepoint2010.jpg
 

Step 8: Looking at previously created permission level output of the application looks like this:

created-permission-level-output-sharepoint2010.jpg

Step 9: Data entering output of the application looks like this:

Data-entering-output-sharepoint2010.jpg
Step 10: Permission level deleted output of the application looks like this:

Permission-level-deleted-output-sharepoint2010.jpg
I hope this article is useful for you.

Reference:

http://www.c-sharpcorner.com/UploadFile/54db21/delete-permission-level-in-sharepoint-2010-programmatically/