This is my code:
public static class Authorisation
{
public static List<PermissionMatrix> GetMatrixData()
{
List<PermissionMatrix> matrixList = new List<PermissionMatrix>()
{
new PermissionMatrix()
{
ActorName= "SubmitterUser",
Moduledata = new List<Module>()
{
new Module()
{
moduleName= ModuleName.GeneralFormModule,
rights=AccessRights.Edit
},
new Module()
{
moduleName=ModuleName.AttachmentsModule,
rights=AccessRights.Edit
},
new Module()
{
moduleName= ModuleName.ProcurementModule,
rights= AccessRights.Edit
},
new Module()
{
moduleName= ModuleName.SupplierModule,
rights=AccessRights.Edit
},
new Module()
{
moduleName= ModuleName.DataPrivacyModule,
rights=AccessRights.Edit
},
new Module()
{
moduleName=ModuleName.SecurityModule,
rights=AccessRights.Edit
},
new Module()
{
moduleName = ModuleName.SecurityModuleUserComments,
rights = AccessRights.Edit
},
new Module()
{
moduleName= ModuleName.SecurityModuleRiskLikelihood,
rights =AccessRights.Edit
},
new Module()
{
moduleName= ModuleName.SecurityModuleRiskSeverity,
rights = AccessRights.Edit
},
new Module()
{
moduleName = ModuleName.SecurityModuleRiskYesNo,
rights = AccessRights.Edit
},
new Module()
{
moduleName = ModuleName.SecurityModuleSecurityComments,
rights = AccessRights.ReadOnly
}
}
},
},
};
return matrixList;
}
}
public class PermissionMatrix
{
public String? ActorName;
public List<Module>? Moduledata;
}
public class Module
{
public ModuleName? moduleName;
public AccessRights? rights;
}
public enum ModuleName
{
GeneralFormModule,
AttachmentsModule,
ProcurementModule,
SupplierModule,
DataPrivacyModule,
SecurityModule,
SecurityModuleRiskYesNo,
SecurityModuleRiskSeverity,
SecurityModuleRiskLikelihood,
SecurityModuleUserComments,
SecurityModuleSecurityComments
}
public enum AccessRights
{
Show = 0,
ReadOnly = 1,
Edit = 2
}
Now I have to write a function which will pass the actorName and ModuleName and I want to get the permission back from the function.
I used extensionmethod like this.
public static class PermissionExtensions
{
public static PermissionMatrix GetAllbyUserName(this IList<PermissionMatrix> coordinates, string user)
{
return coordinates.First(c => String.Equals(c.ActorName, user, StringComparison.CurrentCultureIgnoreCase));
}
public static AccessRights GetAccessPermissionsByUserandModuleName(this IList<PermissionMatrix> coordinates, string user, ModuleName)
{
//return ?
}
}
I tried putting extension methods GetAllbyUserName - I was able to write the function but for GetAccessPermissionsByUserandModuleName - How to write a concise function ?
CodePudding user response:
First a warning: you are using Enumerable.First
which throws an exception if there's no matching PermissionMatrix
. You should use FirstOrDefault
which returns the default value(null
).
Also note that i changed the return type of your extension method to AccessRights?
since it's possible that no one matches, so you have to return Nullable<AccessRights>
:
public static AccessRights? GetAccessPermissionsByUserandModuleName(this IList<PermissionMatrix> coordinates, string user, ModuleName module)
{
return coordinates
.FirstOrDefault(c => string.Equals(c?.ActorName, user, StringComparison.CurrentCultureIgnoreCase))?
.Moduledata?
.Where(m => m.moduleName == module)
.Select(m => m.rights)
.FirstOrDefault();
}