public bool RemoveUserFromGroups(string username, List <byte> groupIDs) { if (groupIDs == null || groupIDs.Count == 0) { return(true); } using (CPSecurityEntities context = new CPSecurityEntities()) { foreach (var m in from m in context.SecurityGroupMemberships where m.UserName == username && groupIDs.Contains(m.GroupId) select m) { context.DeleteObject(m); } try { context.SaveChanges(); return(true); } catch (ConstraintException) { return(false); } } }
/// <summary> /// Deletes a security user profile from the datastore. /// </summary> /// <param name="context">The datastore entity context to use.</param> /// <param name="username">The username of the profile to delete.</param> public static void DeleteSecurityUserProfile(this CPSecurityEntities context, string username) { var match = context.SecurityUserProfiles.FirstOrDefault(p => p.UserName == username); if (match != null) { try { context.DeleteObject(match); context.SaveChanges(); } catch (OptimisticConcurrencyException) { context.Refresh(RefreshMode.ClientWins, match); context.DeleteObject(match); } } }
/// <summary> /// Checks whether or not a login token exists and is valid for authentication. /// </summary> /// <param name="tokenID">The unique identifier of the token to validate.</param> /// <returns>The login name of the user attempting to authenticate with the specified token, if valid; otherwise, null.</returns> /// <remarks>The login token is automatically discarded once validated.</remarks> public string ValidateToken(Guid tokenID) { string username = null; if (tokenID != null) { using (CPSecurityEntities context = new CPSecurityEntities()) { var token = context.SecuritySsoTokens.FirstOrDefault(t => t.TokenId == tokenID); if (token != null) { if (DateTime.Now <= token.Created.Add(Properties.Settings.Default.TokenValidationWindow)) { username = token.UserName; } context.DeleteObject(token); context.SaveChanges(); } } } return(username); }
public bool DeleteGroup(string groupName) { using (CPSecurityEntities context = new CPSecurityEntities()) { var match = context.SecurityGroups.FirstOrDefault(g => g.GroupName == groupName); if (match == null) { return(true); } context.DeleteObject(match); try { return(context.SaveChanges() > 0); } catch (OptimisticConcurrencyException) { return(false); } catch (ConstraintException) { return(false); } } }