protected void btnSave_Click(object sender, EventArgs e) { lblError.Text = string.Empty; lblSave.Text = string.Empty; lblError.Visible = false; lblSave.Visible = false; try { using (TransactionScope scope = new TransactionScope()) { SecRole role = SecRoleManager.GetByRoleId(Convert.ToInt32(hdId.Value)); role.RoleName = txtRoleName.Text; role.RoleDescription = txtRoleDescription.Text; role.Status = ddlStatus.SelectedValue == "1" ? true : false; //role.isClient = cbxIsclient.Checked; SecRoleManager.Save(role); btnCancel_Click(null, null); lblSave.Visible = true; lblSave.Text = "Record Saved Successfully."; //Label1.Text = "Record Saved"; scope.Complete(); } } catch (Exception ex) { lblError.Visible = true; lblError.Text = "Record Not Saved !!!"; } }
public int DeactivateRole(int roleId) { SecRole secRole = _repository.RetrieveSecRole(roleId); secRole.DeletedAt = DateTime.Now; secRole.SecRolePermission.Clear(); return(_repository.UpdateRole(secRole)); }
protected void btnSave_Click(object sender, EventArgs e) { int roleID = Convert.ToInt32(ViewState["RoleID"]); SecRole role = null; Page.Validate("Role"); if (!Page.IsValid) { return; } clientID = Core.SessionHelper.getClientId(); if (roleID == 0) { role = new SecRole(); role.ClientID = clientID; role.Status = true; } else { role = SecRoleManager.GetByRoleId(roleID); } if (role != null) { role.RoleDescription = txtRoleDescription.Text; role.RoleName = txtRoleName.Text; try { using (TransactionScope scope = new TransactionScope()) { role = SecRoleManager.Save(role); SecRoleModuleManager.deleteRoleModules(clientID, roleID); saveRoleModules(clientID, roleID); saveRoleActions(clientID, roleID); // complete transaction scope.Complete(); } // return user to grid showGridPanel(); doBind(); } catch (Exception ex) { Core.EmailHelper.emailError(ex); } } }
protected void gvClientRoles_RowCommand(object sender, GridViewCommandEventArgs e) { int roleID = Convert.ToInt32(e.CommandArgument); SecRole role = null; if (e.CommandName == "DoEdit") { role = SecRoleManager.GetByRoleId(roleID); if (role != null) { ViewState["RoleID"] = roleID.ToString(); showEditPanel(); lbtnSelectAll.Visible = true; txtRoleDescription.Text = role.RoleDescription; txtRoleName.Text = role.RoleName; if (role.ClientID == null) { // prevent system wide roles from being edited by user txtRoleDescription.Enabled = false; txtRoleName.Enabled = false; } else { txtRoleDescription.Enabled = true; txtRoleName.Enabled = true; } bindRoleModules(roleID); bindRoleActions(roleID); } } else if (e.CommandName == "DoDelete") { role = SecRoleManager.GetByRoleId(roleID); if (role != null) { role.Status = false; SecRoleManager.Save(role); bindRoleModules(roleID); } } }
protected void btnSave_Click(object sender, EventArgs e) { int roleID = Convert.ToInt32(ViewState["RoleID"]); SecRole role = null; Page.Validate("Role"); if (!Page.IsValid) return; clientID = Core.SessionHelper.getClientId(); if (roleID == 0) { role = new SecRole(); role.ClientID = clientID; role.Status = true; } else { role = SecRoleManager.GetByRoleId(roleID); } if (role != null) { role.RoleDescription = txtRoleDescription.Text; role.RoleName = txtRoleName.Text; try { using (TransactionScope scope = new TransactionScope()) { role = SecRoleManager.Save(role); SecRoleModuleManager.deleteRoleModules(clientID, roleID); saveRoleModules(clientID, roleID); saveRoleActions(clientID, roleID); // complete transaction scope.Complete(); } // return user to grid showGridPanel(); doBind(); } catch (Exception ex) { Core.EmailHelper.emailError(ex); } } }
public Role(SecRole role) { this.Permissions = new List <Permission>(); this.CheckboxPermissions = new List <Permission>(); this.RoleCode = role.RoleCode; this.RoleId = role.RoleId; this.RoleLabel = role.RoleLabel; this.IsActive = (role.DeletedAt == null); foreach (var rolePermission in role.SecRolePermission) { if ((rolePermission.DeletedAt == null) && (rolePermission.Permission.DeletedAt == null)) { Permissions.Add(new Permission(rolePermission.Permission)); } } }
public static SecRole Save(SecRole secRole) { if (secRole.RoleId == 0) { secRole.CreatedBy = Convert.ToInt32(HttpContext.Current.User.Identity.Name); secRole.CreatedMachineIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); secRole.CreatedOn = DateTime.Now; DbContextHelper.DbContext.Add(secRole); } secRole.UpdatedBy = Convert.ToInt32(HttpContext.Current.User.Identity.Name); secRole.UpdatedMachineIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); secRole.UpdatedOn = DateTime.Now; DbContextHelper.DbContext.SaveChanges(); return(secRole); }
public int CreateRole(Role role) { SecRole secRole = new SecRole() { RoleCode = role.RoleCode, RoleLabel = role.RoleLabel, CreatedAt = DateTime.Now }; foreach (Permission permission in role.Permissions) { SecRolePermission secRolePermission = new SecRolePermission(); secRolePermission.PermissionId = permission.PermissionId; secRole.SecRolePermission.Add(secRolePermission); } return(_repository.CreateRole(secRole)); }
protected void gvData_RowCommand(object sender, GridViewCommandEventArgs e) { //lblError.Text = string.Empty; lblError.Visible = false; lblSave.Visible = false; if (e.CommandName.Equals("DoEdit")) { pnlEdit.Enabled = true; pnlList.Enabled = false; pnlEdit.Visible = true; lbnNew.Visible = false; SecRole role = SecRoleManager.GetByRoleId(Convert.ToInt32(e.CommandArgument)); hdId.Value = role.RoleId.ToString(); txtRoleName.Text = role.RoleName; txtRoleDescription.Text = role.RoleDescription; ddlStatus.SelectedValue = role.Status == true ? "1" : "0"; //cbxIsclient.Checked = role.isClient ?? false; ddlStatus.Focus(); btnSave.Enabled = true; } else if (e.CommandName.Equals("DoDelete")) { try { using (TransactionScope scope = new TransactionScope()) { pnlList.Enabled = true; pnlEdit.Enabled = false; SecRole role = SecRoleManager.GetByRoleId(Convert.ToInt32(e.CommandArgument)); role.Status = false; SecRoleManager.Save(role); scope.Complete(); } DoBind(); // lblSave.Visible = true; lblSave.Text = "Role has been deleted."; } catch (Exception ex) { lblError.Visible = true; lblError.Text = "Role was not deleted!"; } } }
/// <summary> /// Creates a new role. /// </summary> /// <param name="role"/> /// <returns/> public async Task CreateAsync(SecRole role) { try { // Verifico che esista il gruppo Caravan a cui apparterrà il ruolo. await SecurityRepository.GetGroupByNameAsync(AppName, role.GroupName); } catch (SecGroupNotFoundException) { // Se no, lo creo. await SecurityRepository.AddGroupAsync(AppName, new SecGroup { Name = role.GroupName }); } // Dopodiché, sicuro che il gruppo ci sia, aggiungo anche il ruolo. await SecurityRepository.AddRoleAsync(AppName, role.GroupName, role); }
public Role RetrieveRole(int roleId) { SecRole secRole = _repository.RetrieveSecRole(roleId); return(new Role(secRole)); }
/// <summary> /// Deletes a role. /// </summary> /// <param name="role"/> /// <returns/> public Task DeleteAsync(SecRole role) => SecurityRepository.RemoveRoleAsync(AppName, role.GroupName, role.Name);
/// <summary> /// Updates a role. /// </summary> /// <param name="role"/> /// <returns/> public Task UpdateAsync(SecRole role) => SecurityRepository.UpdateRoleAsync(AppName, role.GroupName, role.Name, new SecRoleUpdates { Name = role.Name, Description = role.Description, Notes = role.Notes });
public int UpdateRole(SecRole secRole) { context.SecRole.Update(secRole); return(context.SaveChanges()); }
public int CreateRole(SecRole secRole) { context.SecRole.Add(secRole); context.SaveChanges(); return(secRole.RoleId); }
public int UpdateRole(Role uiRole) { SecRole oldRole = _repository.RetrieveSecRole(uiRole.RoleId); SecRole updatedRole = new SecRole { RoleId = oldRole.RoleId, RoleCode = oldRole.RoleCode, RoleLabel = uiRole.RoleLabel, UpdatedAt = DateTime.Now }; HashSet <int> oldPermissionIdSet = new HashSet <int>(oldRole.SecRolePermission.Select(p => p.PermissionId)); HashSet <int> newPermissionIdSet = new HashSet <int>(uiRole.Permissions.Select(p => p.PermissionId)); HashSet <int> redundantPermissionIdSet = new HashSet <int>(); HashSet <int> softDeletePermissionIdSet = new HashSet <int>(); HashSet <int> addToPermissionIdSet = new HashSet <int>(); foreach (int oldPermissionId in oldPermissionIdSet) { if (newPermissionIdSet.Contains(oldPermissionId)) { // If the old permission is in the new permission set, // mark it as redundant because this sec_role_permission // row is already in the database. redundantPermissionIdSet.Add(oldPermissionId); } else { // If the old permission set is not in the new permission set, // mark it for soft deletion softDeletePermissionIdSet.Add(oldPermissionId); } } foreach (int newPermissionId in newPermissionIdSet) { // If the new permission is not in the old permission set, // mark it for addition. if (oldPermissionIdSet.Contains(newPermissionId) == false) { addToPermissionIdSet.Add(newPermissionId); } } /////////////////////////////////////////////////////////////////////////////// // Redundant or Reactivated Permissions /////////////////////////////////////////////////////////////////////////////// foreach (int redundantPermissionId in redundantPermissionIdSet) { foreach (SecRolePermission oldSecRolePerm in oldRole.SecRolePermission) { if (oldSecRolePerm.PermissionId == redundantPermissionId) { if (oldSecRolePerm.DeletedAt == null) { // If DeletedAt IS NULL, do not add this SecRolePermission to the newly // created role because there haven't been any changes. } else { // If DeletedAt IS NOT NULL, this SecRolePermission needs to be // reactivated by setting DeletedAt to NULL and UpdatedAt to DateTime.Now SecRolePermission reactivatedSecRolePerm = new SecRolePermission { RolePermissionId = oldSecRolePerm.RolePermissionId, RoleId = oldSecRolePerm.RoleId, PermissionId = oldSecRolePerm.PermissionId, CreatedAt = oldSecRolePerm.CreatedAt, DeletedAt = null, UpdatedAt = DateTime.Now }; updatedRole.SecRolePermission.Add(reactivatedSecRolePerm); } } } } /////////////////////////////////////////////////////////////////////////////// // SOFT DELETES // -- SecRolePermission in old SecRole but not in new UIModel.Row // -- set DeletedAt to DateTime.Now /////////////////////////////////////////////////////////////////////////////// foreach (int softDeletePermissionId in softDeletePermissionIdSet) { foreach (SecRolePermission oldSecRolePerm in oldRole.SecRolePermission) { if (oldSecRolePerm.PermissionId == softDeletePermissionId) { SecRolePermission reactivatedSecRolePerm = new SecRolePermission { RolePermissionId = oldSecRolePerm.RolePermissionId, RoleId = oldSecRolePerm.RoleId, PermissionId = oldSecRolePerm.PermissionId, CreatedAt = oldSecRolePerm.CreatedAt, DeletedAt = DateTime.Now, UpdatedAt = oldSecRolePerm.UpdatedAt }; updatedRole.SecRolePermission.Add(reactivatedSecRolePerm); } } } /////////////////////////////////////////////////////////////////////////////// // NEW PERMISSIONS // UIModel.RolePermission in new UIModel.Role but not in old SecRole // Add to SecRole's SecRolePermission collection before saving new version // of SecRole to database. /////////////////////////////////////////////////////////////////////////////// foreach (int addToPermissionId in addToPermissionIdSet) { SecRolePermission newSecRolePerm = new SecRolePermission { RoleId = updatedRole.RoleId, PermissionId = addToPermissionId }; updatedRole.SecRolePermission.Add(newSecRolePerm); } return(_repository.UpdateRole(updatedRole)); }
public static void Delete(SecRole role) { DbContextHelper.DbContext.DeleteObject(role); }