So short answer is yes but you may have to justify this to security team
Here is one i copied shamelessly from one of the gist repo available
public class UnmanagedPermissionSet{
private static void ensureUnmanagedPermissionSet(){
PermissionSet ps = null;
try{
ps = [SELECT Id, Label, (SELECT SObjectType FROM ObjectPerms) FROM PermissionSet WHERE Name = 'EnableEdit'];
}
catch(QueryException qe){
ps = new PermissionSet(Name = 'EnableEdit', Label = 'Enable edit on Standard Objects');
}
upsert ps;
Map<String,ObjectPermissions> oPerms = new Map<String,ObjectPermissions>();
if(ps.ObjectPerms != null && !ps.ObjectPerms.isEmpty()){
for(ObjectPermissions oPerm : ps.ObjectPerms){
oPerms.put(oPerm.SObjectType,oPerm);
}
}
ObjectPermissions campaignPerm = oPerms.get('Campaign');
if(campaignPerm == null){
campaignPerm = new ObjectPermissions(SObjectType='Campaign', ParentId=ps.Id);
oPerms.put('Campaign',campaignPerm);
}
campaignPerm.PermissionsRead = true;
campaignPerm.PermissionsCreate = true;
campaignPerm.PermissionsEdit = true;
campaignPerm.PermissionsDelete = true;
ObjectPermissions contactPerm = oPerms.get('Contact');
if(contactPerm == null){
contactPerm = new ObjectPermissions(SObjectType='Contact', ParentId=ps.Id);
oPerms.put('Contact',contactPerm);
}
contactPerm.PermissionsRead = true;
contactPerm.PermissionsCreate = true;
contactPerm.PermissionsEdit = true;
contactPerm.PermissionsDelete = true;
upsert oPerms.values();
}
}