Итак, я создаю веб-приложение для отслеживания праздников в компании, у которой есть разные сайты. Мне нужно отобразить список сотрудников на моей странице администратора, но я только хочу, чтобы менеджеры / администраторы просматривали список сотрудников, связанных с ними, на основе их сайтов (у них может быть один или несколько).
На своей странице администратора я создал таблицу сотрудников из моей базы данных, в которой отображаются все сотрудники и ссылки на их данные в каждой строке.
Контроллер:
public ActionResult Index(int? page, string searchString)
{
var employees = db.Employees.Include(e => e.Area).Include(e => e.Discipline).Include(e => e.Shift).Include(e => e.Site).Include(e => e.UserRole);
return View(employees.ToList().ToPagedList(page ?? 1, 10 ));
}
Модель:
public partial class Employee
public Employee()
{
this.HolidayRequestForms = new HashSet<HolidayRequestForm>();
}
public int EmployeeID { get; set; }
public string FullName { get; set; }
[Required(ErrorMessage = "This Field is Required")]
public string EmailID { get; set; }
[Required(ErrorMessage = "This Field is Required")]
public string Password { get; set; }
public System.DateTime StartDate { get; set; }
public int RoleID { get; set; }
public int ShiftID { get; set; }
public int AreaID { get; set; }
public int DisciplineID { get; set; }
public int SiteID { get; set; }
public int ALCategory { get; set; }
public Nullable<int> HoursTaken { get; set; }
public Nullable<int> AwardedLeave { get; set; }
public Nullable<int> TotalHoursThisYear { get; set; }
public int HoursCarriedForward { get; set; }
public Nullable<int> EntitlementRemainingThisYear { get; set; }
public string Comments { get; set; }
public int SickLeaveTaken { get; set; }
public Nullable<int> SickLeaveEntitlement { get; set; }
public Nullable<int> SickLeaveEntitlementRemaining { get; set; }
public int StudyLeaveEntitlement { get; set; }
public int StudyLeaveTaken { get; set; }
public Nullable<int> StudyLeaveRemaining { get; set; }
public int ExamLeaveTaken { get; set; }
public int ForceMajeure { get; set; }
public int BereavementLeaveTaken { get; set; }
public int MaternityLeaveTaken { get; set; }
public int ParentalLeaveTaken { get; set; }
public int AdoptionLeaveTaken { get; set; }
public string LoginErrorMessage { get; set; }
public virtual Area Area { get; set; }
public virtual Discipline Discipline { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<HolidayRequestForm> HolidayRequestForms { get; set; }
public virtual Shift Shift { get; set; }
public virtual Site Site { get; set; }
public virtual UserRole UserRole { get; set; }
}
And View (только раздел таблицы):
<table class="table table-striped">
<tr>
<th>
Name
</th>
<th>
Start Date
</th>
<th>
Area
</th>
<th>
Discipline
</th>
<th>
Site Name
</th>
<th>
AL Category
</th>
<th>
Hours CF
</th>
<th>
Awarded Leave
</th>
<th>
Total Hours This Year
</th>
<th>
Hours Taken
</th>
<th>
Hours Remaining
</th>
<th>
Role
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.StartDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Area.Area1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Discipline.Discipline1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Site.SiteName)
<td>
@Html.DisplayFor(modelItem => item.ALCategory)
</td>
<td>
@Html.DisplayFor(modelItem => item.HoursCarriedForward)
</td>
<td>
@Html.DisplayFor(modelItem => item.AwardedLeave)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalHoursThisYear)
</td>
<td>
@Html.DisplayFor(modelItem => item.HoursTaken)
</td>
<td>
@Html.DisplayFor(modelItem => item.EntitlementRemainingThisYear)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserRole.RoleName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.EmployeeID }, new { @class = "btn-xs btn-warning glyphicon glyphicon-pencil" })
@Html.ActionLink("Details", "Details", new { id = item.EmployeeID }, new { @class = "btn-xs btn-info glyphicon glyphicon-info-sign" })
@Html.ActionLink("Delete", "Delete", new { id = item.EmployeeID }, new { @class = "btn-xs btn-danger glyphicon glyphicon-trash" })
</td>
</tr>
}
</table>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
Мне бы хотелось, чтобы только администраторы / администраторы, назначенные определенным сайтам, могли просматривать только эти сайты, но не более. Обычные пользователи не имеют доступа к этой странице.
Можно ли это сделать в MVC ??