This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import auth | |
from django.contrib.auth.middleware import MiddlewareMixin | |
from django.http import HttpResponseForbidden | |
class AutomaticUserLoginMiddleware(MiddlewareMixin): | |
def process_view(self, request, view_func, view_args, view_kwargs): | |
if not AutomaticUserLoginMiddleware._is_user_authenticated(request): | |
user = auth.authenticate(request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.backends import ModelBackend | |
from django.contrib.auth.models import User | |
from .services.ldap import get_LDAP_user | |
class AuthenticationBackend(ModelBackend): | |
def authenticate(self, request, username=None, password=None, **kwargs): | |
# Get the user information from the LDAP if he can be authenticated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
# Application definition | |
AUTHENTICATION_BACKENDS = [ | |
'AutomaticDjangoAuthentication.authentication_backend.AuthenticationBackend', | |
] | |
INSTALLED_APPS = [ | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ... | |
class AuthenticationBackend(ModelBackend): | |
def authenticate(self, request, username=None, password=None, **kwargs): | |
# Get credentials from the query strings | |
username = request.GET.get('username') | |
password = request.GET.get('password') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ldap3 import Server, Connection, ALL | |
LDAP_URL = 'ldap.forumsys.com' | |
# Check user authentication in the LDAP and return his information | |
def get_LDAP_user(username, password): | |
try: | |
server = Server(LDAP_URL, get_info=ALL) | |
connection = Connection(server, | |
'uid={username},dc=example,dc=com'.format( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Application definition | |
MIDDLEWARE = [ | |
'django.middleware.security.SecurityMiddleware', | |
'django.contrib.sessions.middleware.SessionMiddleware', | |
'django.middleware.common.CommonMiddleware', | |
'django.middleware.csrf.CsrfViewMiddleware', | |
'django.contrib.auth.middleware.AuthenticationMiddleware', | |
# Add the new middleware just after the default AuthenticationMiddleware that manages sessions and cookies | |
'AutomaticDjangoAuthentication.authentication_middleware.AutomaticUserLoginMiddleware', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Message { | |
... | |
@ManyToOne(fetch = FetchType.LAZY) | |
@JoinColumn(name = "author_id") | |
private User author; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void logMessages() { | |
// Get all the messages from the database | |
// -> Triggers 1 query | |
Set<Message> messages = messageDao.findAll(); | |
// Map through the N messages to create the DTO with the author display name | |
// -> Triggers 1 query to fetch each author so N queries! | |
messages.stream.map( | |
message -> logger.info( | |
message.getAuthor().getName() + ": " + message.getText() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>com.yannbriancon</groupId> | |
<artifactId>spring-hibernate-query-utils</artifactId> | |
<version>1.0.3</version> | |
</dependency> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import com.yannbriancon.interceptor.HibernateQueryCountInterceptor; | |
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
@Transactional | |
public class NotificationResourceIntTest { | |
@Autowired | |
private HibernateQueryCountInterceptor hibernateQueryCountInterceptor; |
OlderNewer