private void CreateRoleforCompany() { if (String.IsNullOrWhiteSpace(ErrorRoleName) && String.IsNullOrWhiteSpace(ErrorRoleDescription)) { if (this.CheckAllPermissions()) { using (var ctx = new Choukash_Revamp_DemoEntities()) { IList <Permission> permissions = new List <Permission>(); Role user_role = new Role() { name = RoleName, description = RoleDescription, companies_id = UserCompany.id }; for (int i = 0; i < ListPermissions.Count(); i++) { if (ListPermissions[i].IsChecked) { permissions.Add(AllPermissions[i]); } } if (this.NextPage == null && this.Title == "Create Role") { UserPage = new CreateUserViewModel("Create Admin", this.UserCompany, user_role, permissions, this.EventAggregator); this.NextPage = new NavigatePage(UserPage); this.EventAggregator.PublishOnUIThread(NextPage); } else if (this.NextPage == null && this.Title == "Add Role") { ctx.Roles.Add(user_role); CastPermissiontoRolePermission(user_role, permissions, ctx); ctx.SaveChanges(); var sa = ctx.SuperAdmins.Where(a => a.id == UserCompany.superadmin_id).SingleOrDefault <SuperAdmin>(); this.EventAggregator.PublishOnUIThread("Operation complete"); EditPage = new EditProductViewModel(sa, EventAggregator); this.NextPage = new NavigatePage(EditPage); this.EventAggregator.PublishOnUIThread(NextPage); } else if (this.NextPage == null && this.Title == "Add new Role") { ctx.Roles.Add(user_role); CastPermissiontoRolePermission(user_role, permissions, ctx); ctx.SaveChanges(); this.EventAggregator.PublishOnUIThread("Added new Role"); this.EventAggregator.PublishOnUIThread(this); } else { this.EventAggregator.PublishOnUIThread(NextPage); } } } else { ErrorSelectionPermission = "Please Provide Permission for Role"; } } }
private void EditRole() { using (var ctx = new Choukash_Revamp_DemoEntities()) { var role = ctx.Roles.Include(a => a.Role_Permission).Where(a => a.id == UserRole.id).SingleOrDefault <Role>(); role.name = RoleName; role.description = RoleDescription; IList <Permission> permissions = new List <Permission>(); for (int i = 0; i < ListPermissions.Count(); i++) { if (ListPermissions[i].IsChecked) { permissions.Add(AllPermissions[i]); } } foreach (var permission in permissions) { if (!role.Role_Permission.Contains(ctx.Role_Permission.Where (a => a.permissions_id == permission.id && a.roles_id == UserRole.id).SingleOrDefault <Role_Permission>())) { ctx.Role_Permission.Add(new Role_Permission() { roles_id = UserRole.id, permissions_id = permission.id }); } } foreach (var rp in role.Role_Permission.ToList()) { if (!permissions.Contains(AllPermissions.Where(a => a.id == rp.permissions_id).SingleOrDefault <Permission>())) { ctx.Role_Permission.Remove(rp); } } ctx.SaveChanges(); if (this.Title == "Edit Role") { this.EventAggregator.PublishOnUIThread("Operation complete"); var editpage = new EditProductViewModel(ctx.SuperAdmins.Where(a => a.id == UserCompany.superadmin_id).SingleOrDefault <SuperAdmin>(), this.EventAggregator); var tool = new NavigatePage(editpage); this.EventAggregator.PublishOnUIThread(tool); } else if (this.Title == "Edit Existing Role") { this.EventAggregator.PublishOnUIThread("Edited Existing Role"); this.EventAggregator.PublishOnUIThread(this); } } }
private void GetAllPermissions() { using (var ctx = new Choukash_Revamp_DemoEntities()) { foreach (var p in ctx.Permissions) { AllPermissions.Add(p); ListPermissions.Add(new CheckBoxListViewItem(p.menu, false)); } } }