/// <summary> /// Deletes a role with the specified id /// </summary> /// <param name="id">role id</param> /// <returns>success</returns> public bool delete(long id) { using (ACLDataContext u = new ACLDataContext()) { try { u.roles.DeleteAllOnSubmit(u.roles.Where(x => x.id == id)); u.SubmitChanges(); } catch { return false; } return true; } }
/// <summary> /// Adds a new role /// </summary> /// <param name="parent">Parent role</param> /// <param name="form">Role data</param> /// <returns>success</returns> public bool add(long parent, Form_Role_Add form) { role r = new role(); r.date = DateTime.Now; r.parentid = parent; if (parent == 0) { r.parentid = null; } r.name = form["name"].getValue(); using (ACLDataContext a = new ACLDataContext()) { a.roles.InsertOnSubmit(r); a.SubmitChanges(); } return true; }
/// <summary> /// Checks if current user has privilegies to access the given resource /// </summary> /// <param name="resource">Resource name</param> /// <param name="acl">ACL</param> /// <param name="assign">Assig roles and resources to ACL</param> public void checkACL(string resource, CMS_Acl acl, bool assign) { CMS_Login login = new CMS_Login(); if (assign) { using (ACLDataContext DataContext = new ACLDataContext()) { var roles = DataContext.roles .OrderBy(x=>x.parentid) .Select(x=>new{RoleName = x.name, RoleID = x.id,RoleParentId = x.parentid, RoleParentName = x.role1.name}).ToList(); ////var roles = from r in DataContext.roles //// join r2 in DataContext.roles on r.parentid equals r2.id into joined //// from a in joined.DefaultIfEmpty() //// orderby r.parentid //// select new { RoleName = r.name, RoleID = r.id, RoleParentId = r.parentid, RoleParentName = a.role1.name }; Dictionary<long?, CMS_Role> parentals = new Dictionary<long?, CMS_Role>(); foreach (var a in roles) { if (a.RoleParentId != null && parentals.ContainsKey(a.RoleParentId)) { CMS_Role r = new CMS_Role(a.RoleName, parentals[a.RoleParentId]); acl.addRole(r); parentals.Add(a.RoleID, r); } else { CMS_Role r = new CMS_Role(a.RoleName); acl.addRole(r); parentals.Add(a.RoleID, r); } } var resources = from res in DataContext.resources select new { ResourceName = res.name, Action = res.action, Controller = res.controller }; foreach (var a in resources) { acl.addResource(new CMS_Resource(a.Controller + ":" + a.Action)); } var rules = from r in DataContext.roles join cr in DataContext.role_resources on r.id equals cr.rolesid join res in DataContext.resources on cr.resourcesid equals res.id orderby r.id select new { Role = r.name, Controller = res.controller, Action = res.action }; if (rules.Count() > 0) { foreach (var a in rules) { acl.allow(a.Role, a.Controller + ":" + a.Action); } } } } user user; string role; if (login.hasIdentity()) { user = login.getIdentity(); role = this.roles().getById(user.rolesid).name; } else { user = null; role = "guest"; } if (!acl.isAllowed(role, resource)) { if (!login.hasIdentity()) { throw new Exception("You are not logged in! Log in and try again."); } else { //trigger error throw new Exception("You are not allowed to view this datasource!"); //TODO } } }
/// <summary> /// Save changes to the given role /// </summary> /// <param name="form">Role data</param> /// <param name="r">Role</param> /// <returns>success</returns> public bool save(Form_Role_Add form, role r) { role newRole = new role(); newRole.id = r.id; if (r.parentid.HasValue) { newRole.parentid = r.parentid; } newRole.name = form["name"].getValue(); newRole.date = r.date; using (ACLDataContext a = new ACLDataContext()) { a.roles.Attach(newRole, r); a.SubmitChanges(); } return true; }
/// <summary> /// Gets count of roles /// </summary> /// <returns>Count of roles</returns> public int getCount() { using (ACLDataContext a = new ACLDataContext()) { return a.roles.Count(); } }
/// <summary> /// Gets role by role name /// </summary> /// <param name="name">role name</param> /// <returns>Role</returns> public role getByName(string name) { using (ACLDataContext c = new ACLDataContext()) { try { return c.roles.Where(x => x.name == name).Single(); } catch (Exception) { return null; } } }
/// <summary> /// Returns category identified by the specified ID /// </summary> /// <param name="id">User id</param> /// <returns>Role represented by the given id</returns> public role getById(long id) { using (ACLDataContext a = new ACLDataContext()) { try { var data = a.roles .Where(x => x.id == id) .Single(); return data; } catch (Exception) { return null; } } }
/// <summary> /// Gets all roles /// </summary> /// <returns></returns> public List<role> getAll() { using (ACLDataContext a = new ACLDataContext()) { return a.roles.ToList(); } }
/// <summary> /// List of roles /// </summary> /// <param name="id">parent role (0 for root)</param> /// <param name="a">DataContext</param> /// <param name="start">How many roles have to be skipped</param> /// <param name="count">The maximum amount of returned roles</param> /// <returns>List of roles</returns> public List<role> get(long id, ACLDataContext a, int start, int count) { using (a) { if (id > 0) { var data = a.roles.Where(x => x.parentid == id).Skip(start).Take(count); return data.ToList(); } else if (id == 0) { var data = a.roles.Where(x => x.parentid == null).Skip(start).Take(count); return data.ToList(); } else { var data = a.roles.Skip(start).Take(count); return data.ToList(); } } }