public override void AddUserToRole(Telerik.Sitefinity.Security.Model.User user, Telerik.Sitefinity.Security.Model.Role role) { var link = this.CreateUserLink(); link.UserId = user.Id; link.MembershipManagerInfo = this.UserManagerInfo; var scope = this.GetContext(); Telerik.Sitefinity.Security.Model.Role existingRole = null; // Try get existing role - if a user is already added in this role, the role will exist in context of Sitefinity // When first time assigning the role this will throw exception - Item you are trying to access, no longer exists. try { existingRole = scope.GetItemById <Telerik.Sitefinity.Security.Model.Role>(role.Id.ToString()); } catch (Exception ex) { } if (existingRole != null) { link.Role = existingRole; } else { link.Role = role; } scope.Add(link); }
public override Telerik.Sitefinity.Security.Model.Role CreateRole(Guid Id, string roleName) { var role = new Telerik.Sitefinity.Security.Model.Role(); using (var entity = new RolesDbAppContext()) { var roleExist = entity.Roles.Where(r => r.Name == roleName).FirstOrDefault(); if (roleExist != null) { throw new ArgumentException("role name exists", "roleName"); } var roleExistId = entity.Roles.Where(r => r.Id == Id).FirstOrDefault(); if (roleExistId != null) { throw new ArgumentException("Id exists", "Id"); } var newRole = entity.Roles.Create(); newRole.Id = Id; newRole.Name = roleName; entity.Roles.Add(newRole); entity.SaveChanges(); role.Id = Id; role.Name = roleName; role.ApplicationName = this.ApplicationName; return(role); } }
public override Telerik.Sitefinity.Security.Model.Role GetRole(Guid Id) { var newRole = new Telerik.Sitefinity.Security.Model.Role(); using (var entity = new RolesDbAppContext()) { var role = entity.Roles.Where(r => r.Id == Id).FirstOrDefault(); newRole.Id = role.Id; newRole.Name = role.Name; newRole.ApplicationName = this.ApplicationName; } return(newRole); }
public override void Delete(Telerik.Sitefinity.Security.Model.Role item) { using (var entity = new RolesDbAppContext()) { var role = entity.Roles.Where(r => r.Name == item.Name).FirstOrDefault(); var scope = this.GetContext(); try { var itemFromContext = scope.GetItemById(item.GetType(), item.Id.ToString()); if (itemFromContext != null) { scope.Remove(itemFromContext); } } catch (Exception ex) { } entity.Roles.Remove(role); entity.SaveChanges(); } }