asp.net MVC EF 6 Identity only allowed to select 'lower' roles - asp.net-mvc

I have these roles setup within identity and one role is assigned per user
00 - Super Admin
10 - Admin
20 - Staff Manager
30 - Staff
40 - User Manager
50 - User
The aim is to allow a user to change the role of other users below (not equal to) their own user role
so if I could get the first two chars from the Name of the role added to the current user e.g. 30 then the choice of roles that could be applied to other users would be > 30
to get the user state of the current user
var currentUser = await _userManager.GetUserAsync(HttpContext.User);
var currentUserRole = await _userManager.GetRolesAsync(currentUser);
Then something like below that get all the roles, with 'WHERE' clause, or following if statement, first two chars > current users first two chars
var allRoles = _roleManager.Roles.ToList();
Does anyone know of a way that this could be done, or if there is a better idea?
Many Thanks
EDIT:
Ok, I now have this hardcoded to a SelectList in the view, select roles > 30 which is working
ViewBag.allRoles = _roleManager.Roles.Where(a => Convert.ToInt32(a.Name.Remove(2)) > 30).Select(a => new SelectListItem()
{
Value = a.Id.ToString(),
Text = a.Name.Remove(0, 5).ToString(),
}).ToList();
just need to get the current user role name to a string?

I have managed to sort it
var currentUser = await _userManager.GetUserAsync(HttpContext.User);
var currentUserRole = await _userManager.GetRolesAsync(currentUser);
int currentUserRoleLevel = int.Parse(currentUserRole[0].Remove(2));
ViewBag.allRoles = _roleManager.Roles.Where(a => Convert.ToInt32(a.Name.Remove(2)) > currentUserRoleLevel).Select(a => new SelectListItem()
{
Value = a.Id.ToString(),
Text = a.Name.Remove(0, 5).ToString(),
}).ToList();
The question still stands, is there a better way?

Related

Selecting list for drop down ASP.NET MVC

In my database, I have a table called Employee and it has columns EmpNames and EmpId which same EmpId created User table with user levels. I want to get a list of empNames and id's to who are user level equal to the 4.
This is how I got empname list for a drop down list
List<M_Employee> EmpList = db.CreateEmployee.Where(x => x.Status == true).ToList();
List<SelectListItem> EmpDropDown = EmpList.Select(x => new SelectListItem { Text = x.EmpName, Value = x.Id.ToString() }).ToList();
Same way I have tried to query the user level = 4 and tried to join emp table with user table to get the emp names who assigned user levels to 4 but it didn't work.
Here is my code for that
List<int> TopEmp = db.Master_Users.ToList().Where(r => r.EmpId == int.Parse(db.CreateEmployee.Where(x=> x.Id))).ToList().
Can you help me on this?
Firstly, you need to understand how ToList works.
When you call ToList it means that Entity framework will execute the sql statement constructed at that point and retrieve the results into memory.
You generally want to construct your entire query first and then have that query get all the data you want from the database in the format of an object you want by using .Select(x => x.whatever).ToList(). Otherwise you'll be making multiple calls to the database to get bits of data here and there and then joining them or working with them unnecessarily in memory which is slower than having the database do it.
So your first query where you get the select list items can be rewritten like this:
List<SelectListItem> EmpDropDown = db.CreateEmployee
.Where(x => x.Status == true)
.Select(x => new SelectListItem { Text = x.EmpName, Value = x.Id.ToString() })
.ToList()
And from what you've described you should be able to rewrite the 2nd query like this:
List<int> TopEmp = (from u in db.Master_Users
join e in db.CreateEmployee on u.EmpId equals e.Id
where u.Level == 4
select e.Id
).ToList();
This is using a different query syntax but allows to specify the key to join on easily as I don't know how your foreign keys and navigation properties are setup.
I can't see you dbcontext, maybe it is possible to use include too, but for the start try this
List<SelectListItem> EmpDroDown = (from emp in db.CreateEmployee
join usr in db.Master_Users on emp.Id equals usr.EmpId
where emp.Status == true && usr.UserLevel==4
select new SelectListItem { Text = em.EmpName,
Value = emp.Id.ToString() }).ToList();

Trying to use Entity Framework method for multiple adds to database

I have an ASP.NET MVC app using Entity Framework from our SQL Server backend.
Goal is to create ~18 WPackage entries via a foreach loop:
foreach (var dbitem in dbCList)
The code works for a single WPackage entry, but we have a request from the customer to create 300+ WPackages, so trying to use the Entity Framework code for a single "Add" and loop to create 300+ adds.
The T-SQL would be very challenging as there are many keys created on the fly/at row creation, so for activities >> resources, we'd have to insert the activity, grab or remember the activity key, then add resources with that newly created activity key.
Each WPackage (this is the main parent table) could have one or more of the following child table entries:
1+ activities
each activity would have 1+ resource
1+ budgets
1+ Signatures
1+ CostCodes
Our schema or model diagram would be:
WPackage
--Activities
-----Resources (child of Activities)
--CostCodes
--Budgets
--Signatures
The following code fails on:
dbContextTransaction.Commit();
with an error:
The transaction operation cannot be performed because there are pending requests working on this transaction.
[HttpPost]
public ActionResult Copy([Bind(Include = "ID,WBSID,...***fields excluded for brevity")] Package model)
{
if (ModelState.IsValid)
{
try
{
using (var dbContextTransaction = db.Database.BeginTransaction())
{
var dbCList = db.Packages.Join(db.WBS,
*expression omitted for brevity*)
// this dbClist will build about 18 items in the collection for below loop
foreach (var dbitem in dbCList)
{
int testWPID = dbitem;
WPackage prvWP = db.WPackages.Find(dbitem);
int previousWPID = dbitem;
WPackage previousWP = db.WPackages.Find(dbitem);
model.ID = dbitem;
db.WPackages.Add(model);
db.SaveChanges();
var budgets = db.Budgets.Where(i => i.WPID == previousWPID);
foreach (Budget budget in budgets)
{
budget.WPID = model.ID;
db.Budgets.Add(budget);
}
var costCodes = db.CostCodes.Where(i => i.WPID == previousWPID);
foreach (CostCode costCode in costCodes)
{
costCode.WPID = model.ID;
db.CostCodes.Add(costCode);
}
var activities = db.Activities.Where(i => i.WPID == previousWPID);
// *code excluded for brevity*
var previousActivityID = activity.ID;
db.Activities.Add(activity);
db.SaveChanges();
var resources = db.Resources.Where(i => i.ActivityID == previousActivityID);
foreach (Resource resource in resources)
{
resource.WPID = model.ID;
resource.ActivityID = activity.ID;
resource.ActivityNumber = activity.ActivityNumber;
db.Resources.Add(resource);
db.SaveChanges();
}
}
var signatures = db.RolesAndSigs
.Where(i => i.KeyId == previousWPID && i.Type == "WPL")
.OrderBy(i => i.Role)
.OrderBy(i => i.Person);
foreach (RolesAndSig signature in signatures)
{
db.RolesAndSigss.Add(signature);
}
db.SaveChanges();
dbContextTransaction.Commit();
}
}
}
}
I've also tried to have the Commit() run outside the foreach dbitem loop like:
db.SaveChanges();
//dbContextTransaction.Commit();
}
dbContextTransaction.Commit();
...but this returns error of:
[EXCEPTION] The property 'ID' is part of the object's key information and cannot be modified.
The code you posted has some issues that don't make sense, and probably aren't doing what you think they are doing. The crux of the issue you are facing is that Entity Framework tracks all references to entities it loads and associates:
Firstly this code:
int testWPID = dbitem;
WPackage prvWP = db.WPackages.Find(dbitem);
int previousWPID = dbitem;
WPackage previousWP = db.WPackages.Find(dbitem);
prvWP and previousWP will be pointing to the exact same reference, not two copies of the same entity. Be careful when updating either or any other reference retrieved or associated with that same ID. They all point to the same instance. If you do want a stand-alone snaphot reference you can use AsNoTracking().
Next, when you do something like this in a loop:
model.ID = dbitem;
db.WPackages.Add(model);
In the first iteration, "model" is not an entity. It is a deserialized block of data with the Type of the Package entity. As soon as you call .Add(model) that reference will now be pointing to a newly tracked entity reference. In the next loop you are telling EF to change that tracked entity reference's ID to a new value, and that is illegal.
What it looks like you want to do is create a copy of this model for each of the 18 expected iterations. For that what you want to do would be something more like:
foreach (var dbitem in dbCList)
{
var newModel = new WPackage
{
ID = dbItem,
WBSID = model.WBSID,
/// copy across all relevant fields from the passed in model.
};
db.WPackages.Add(newModel);
// ...
}
It would be quite worthwhile to leverage navigation properties for the related entities rather than using explicit joins and trying to scope everything in an explicit transaction with multiple SaveChanges() calls. EF can manage all of the FKs automatically rather than essentially using it as a wrapper for individual ADO CRUD operations.
You will need to be explicit between when you want to "clone" an object reference vs. "copy" a reference. For example, if I have a Customer that has an Address, and Addresses have a Country reference, when I clone a Customer, I will want to clone a new Address record for that Customer, however ensure that the Country reference is copied across. If I have a record for Jack at an 123 Apple Street, London in England, and go to clone Jack to make a record for Jill at the same address, they might be at the same location now, but not always, so I want them to point at different Address records in case Jill moves out. Still, there should only be one record for "England". (Jill may move to a different country, but her address record would just point at a different Country Id)
Wrong:
var jill = context.Customers.Single(c => c.Name == "Jack");
jill.Name = "Jill";
context.Customers.Add(jill);
This would attempt to rename Jack into Jill, then "Add" the already tracked instance, resulting in an exception.
Will work, but still Wrong:
var jack = context.Customers.AsNoTracking().Single(c => c.Name == "Jack");
var jill = jack;
jill.Name = "Jill";
context.Customers.Add(jill);
This would technically work by loading Jack as an untracked entity, and would save Jill as a new record with a new Id. However this is potentially very confusing. Depending on how the AddressId/Address is referenced we could end up with Jack and Jill referencing the same single Address record. Bad if you want Jack and Jill to have different addresses.
Right:
var jack = context.Customers
.Include(c => c.Address)
.ThenInclude(a => a.Country)
.Single(c => c.Name == "Jack");
var jill = new Customer
{
Name = "Jill",
// copy other fields...
Address = new Address
{
StreetNumber = jack.Address.StreetNumber,
StreetName = jack.Address.StreetName,
Country = jack.Address.Country
}
};
context.Customers.Add(jill);
The first detail is to ensure when we load Jack that we eager load all of the related details we will want to clone or copy references to. We then create a new instance for Jill, copying the values from Jack, including setting up a new Address record. The Country reference is copied across as there should only be ever a single record for "England".
Edit: For something like a roll-over scenario if you have a package by year, let's use the example of a Package class below:
public class Package
{
[Key]
public int PackageId { get; set; }
[ForeignKey("PackageType")]
public int PackageTypeId { get; set; }
public int Year { get; set; }
// .. More package related details and relationships...
public virtual PackageType PackageType { get; set; }
}
A goal might be to make a new Package and related data for Year 2022 from the data from 2021, and apply any changes from a view model passed in.
Find is a poor choice for this because Find wants to locate data by PK. If you're method simply passes an entity to be copied from (I.e. the data from 2021) then this can work, however if you have modified that data from 2021 to represent values you want for 2022 that could be dangerous or misleading within the code. (We don't want to update 2021's data, we want to create a new record set for 2022) To make a new Package for 2022 we just need the updated data to make up that new item, and a way to identify a source for what to use as a template. That identification could be the PK of the row to copy from (ProductId), or derived from the data passed in. (ProductTypeId, and Year-1) In both cases if we want to consider related data with the "copy from" product then it would be prudent to eager load that related data in one query rather than going back to the database repeatedly. Find cannot accommodate that.
For instance if I want to pass data to make a new product I pass a ProductTypeId, and a Year along with any values to use for the new structure. I can attempt to get a copy of the existing year to use as a template via:
var existingProduct = context.Products
.Include(x => x.Activities) // Eager load related data.
.Include(x => x.CostCodes)
// ...
.Single(x => x.ProductTypeId == productTypeId && x.Year = year - 1);
or if I passed a ProductId: (such as if I could choose to copy the data from a selected year like 2020 instead)
var existingProduct = context.Products
.Include(x => x.Activities)
.Include(x => x.CostCodes)
// ...
.Single(x => x.ProductId == copyFromProductId);
Both of these examples expect to find one, and only one existing product. If the request comes in with values that it cannot find a row for, there would be an exception which should be handled. This would fetch all of the existing product information that we can copy from, alongside any data that was passed into the method to create a new Product.

Return table records based on user role / table Join failing - linq query

I am building a MVC 5 (EF6) application that has a photographer table that's linked to the users table.
I identify a photographer by setting a role on the user account. The photographer table holds additional information not table. I have different types of users accessing the system.
I need to get the photographer table records based on the user being in the photographer role. I have the following solution but it comes back with the following error message when i try to access query result.
Unable to create a constant value of type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole'. Only primitive types or enumeration types are supported in this context.
var photogpraherRole = db.Roles.Single(r => r.Name == "photographer");
var users = photogpraherRole.Users.ToList();
var photographersList =
from photographer in db.Photographers
join user in users on photographer.User.Id equals user.UserId
orderby photographer.Priority
select photographer;
foreach (var photographer in photographersList)
{
var photographerName = photographer.User.FirstName;
}
After some research and the help from #Shawn Yan, this is the solution i came up with.
var photographerRole = db.Roles.SingleOrDefault(m => m.Name == "photographer");
var disabledRole = db.Roles.SingleOrDefault(m => m.Name == "disabled");
var users = db.Users.Where(m => m.Roles.All(r => r.RoleId == photographerRole.Id && r.RoleId != disabledRole.Id)).ToList();
var photographersList =
from photographer in db.Photographers.ToList()
join user in users on photographer.User.Id equals user.Id
where photographer.Location == booking.location.Location
orderby photographer.Priority
select photographer;
var photographers = photographersList.ToList();

How to pass UserProfile model to view?

I am trying to implement simple membership in my application. My problem is that I want to be able to display the data in the userprofile table for the current user but I dont know how to select it from the DB
I have tried this but I am getting an error:
UserProfile UserP = new UserProfile();
ViewBag.Message = User.Identity.Name;
return View();
UserP = (from r in up.UserName
where up.UserName == User.Identity.Name.ToString()
select r).ToList().FirstOrDefault();
return View(UserP);
Here is the error:
Error 1 Cannot implicitly convert type 'char' to 'MvcApplication5.Models.UserProfile' C:\Users\user\Desktop\MvcApplication5\MvcApplication5\Controllers\HomeController.cs 31 32 MvcApplication5
If I got you right (your code is little bit broken, it has two returns, so I assume there is just two pieces of code), try this:
UserP = (from r in up
where up.UserName == User.Identity.Name.ToString()
select r).FirstOrDefault();
Just get rid of up.UserName in your query. ToList() is also not needed.
P.S. For the future:
I also suggest you adding another column called LoweredUserName and perform checking in the following way:
where up.LoweredUserName == User.Identity.Name.ToString().ToLower()
Here is how you can access the UserProfile.
var context = new UsersContext();
var username = User.Identity.Name;
var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
return View(user);
For more on customizing the UserProfile and accessing it read this article.

Umbraco Get Users by User Type

If I have a list of user types (both ID and name), how can I get the users that belong to that user type (not members)? There doesn't seem to be any methods for User[] userList = User.Get ByType
Using linq you should be able to get what you're looking for. Here's an example of how to get Users based solely on the alias of the User Type:
string[] userTypeAliases = new string[] { "writer", "editor" };
var userTypes = umbraco.BusinessLogic.UserType.GetAllUserTypes()
.Where(ut => userTypeAliases.Contains(ut.Alias));
var users = umbraco.BusinessLogic.User.getAll()
.Where(u => userTypes.Contains(u.UserType));
You can do it using IDs like this:
var userType = UserType.GetUserType(1);
var users = User.getAll().Where(u => u.UserType == userType);

Resources