Hope this helps!
public void addPermissionToGroup()
{
SPSite site = new SPSite("http://mysite:5050/");
SPWeb spWeb = site.OpenWeb();
string permissionName = "Read";
string groupName = "Project Manager";
try
{
spWeb.AllowUnsafeUpdates = true;
SPRoleAssignment roleAssignment = new SPRoleAssignment(spWeb.SiteGroups[groupName]);
roleAssignment.RoleDefinitionBindings.Add(spWeb.RoleDefinitions[permissionName]);
if (!spWeb.HasUniqueRoleAssignments)
spWeb.BreakRoleInheritance(false);
spWeb.RoleAssignments.Add(roleAssignment);
spWeb.Update();
}
catch (Exception _exception)
{
throw _exception;
}
finally
{
spWeb.AllowUnsafeUpdates = false;
}
}
For adding user to Group:
public void AddUsers(string groupname, string username)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Gets a new security context using SHAREPOINT\system
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb thisWeb = site.OpenWeb())
{
thisWeb.AllowUnsafeUpdates = true;
SPUser Name = thisWeb.EnsureUser(username);
thisWeb.Groups[groupname].AddUser(Name);
thisWeb.AllowUnsafeUpdates = false;
}
}
});
}
catch (Exception ex)
{
//Log error here.
}
}
Set GroupA as owner of GroupB
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite site = new SPSite("http://site02"))
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPGroup visitors = web.SiteGroups["GroupB"];
SPGroup members = web.SiteGroups["GroupA"];
visitors.Owner = (SPMember)members;
visitors.Update();
web.AllowUnsafeUpdates = false;
}
});