/// <summary> /// Method to select single User by id. /// </summary> /// <returns>An User entity or null if not found.</returns> public UserEntity SingleIdOrNull(IQueryable<UserEntity> query, UserOptionsSelect op) { UserEntity item = null; try { item = query.SingleOrDefault(x => x.UserId == op.PrimaryKey); } catch (ArgumentNullException e) { log.Error("User single ID not found !", e); return null; } catch (Exception e) { log.Fatal("User single ID not found !", e); return null; } // Check if user is found, return null instead of default. if (item != null && item.UserId == 0) { return null; } return item; }
/// <summary> /// Method to select an User by name. /// </summary> /// <returns>An User entity or null if not found.</returns> public UserEntity SingleNameOrNull(IQueryable<UserEntity> query, UserOptionsSelect op) { // Select user by name. UserEntity item = null; try { // Search not case sensible. item = query.SingleOrDefault(x => x.Name.ToLower() == op.Name.ToLower()); } catch (ArgumentNullException e) { log.Error(e.Output(), e); return null; } catch (Exception e) { log.Fatal(e.Output(), e); return null; } // Check if user is found, return null instead of default. if (item != null && item.UserId == 0) { return null; } // Check password if required. if (op.CheckPassword && (op.Password != item.Password)) { return null; } return item; }
/// <summary> /// Method to select an User. /// </summary> /// <param name="op">Users entities select options to perform query.</param> /// <returns>A User entity or null if not found.</returns> public UserEntity Select(UserOptionsSelect op) { // Initialize query. IQueryable<UserEntity> query = Connector.Users; // Load AclGroups dependencies if required. if (op.IsDependOn(EnumEntitiesDependencies.UsersInAclGroups)) { query = query.Include(x => x.UsersInAclGroups); } if (op.PrimaryKey > 0) { return SingleIdOrNull(query, op); } if (!op.Name.IsNullOrWhiteSpace()) { return SingleNameOrNull(query, op); } if (!op.Email.IsNullOrWhiteSpace()) { return SingleEmailOrNull(query, op); } return null; }
/// <summary> /// Method to select an User by name. /// </summary> /// <returns>An User entity or null if not found.</returns> public UserEntity SingleEmailOrNull(IQueryable<UserEntity> query, UserOptionsSelect op) { // Select user by name. UserEntity item = null; try { item = query.SingleOrDefault(x => x.Email == op.Email); } catch (ArgumentNullException e) { log.Error("User single EMAIL not found !", e); return null; } catch (Exception e) { log.Fatal("User single EMAIL not found !", e); return null; } // Check if user is null. if (item == null) { return null; } // Check if user is found, return null instead of default. if (item.UserId == 0) { return null; } // Check password if required. if (op.CheckPassword && (op.Password != item.Password)) { return null; } return item; }