code: def service_login(self):
          try:
              service_pattern = ServicePattern.validate(self.service)
              service_pattern.check_user(self.user)
              if self.request.session.get("warn", True) and not self.warned:
                  messages.add_message(
                      self.request,
                      messages.WARNING,
                      _(u"Authentication has been required by service %(name)s (%(url)s)") %
                      {'name': service_pattern.name, 'url': self.service}
                  )
                  if self.ajax:
                      data = {"status": "error", "detail": "confirmation needed"}
                      return json_response(self.request, data)
                  else:
                      warn_form = forms.WarnForm(initial={
                          'service': self.service,
                          'renew': self.renew,
                          'gateway': self.gateway,
                          'method': self.method,
                          'warned': True,
                          'lt': self.request.session['lt'][-1]
                      })
                      return render(
                          self.request,
                          settings.CAS_WARN_TEMPLATE,
                          utils.context({'form': warn_form})
                      )
              else:
                  list(messages.get_messages(self.request))
                  redirect_url = self.user.get_service_url(
                      self.service,
                      service_pattern,
                      renew=self.renewed
                  )
                  if not self.ajax:
                      return HttpResponseRedirect(redirect_url)
                  else:
                      data = {"status": "success", "detail": "auth", "url": redirect_url}
                      return json_response(self.request, data)
          except ServicePattern.DoesNotExist:
              error = 1
              messages.add_message(
                  self.request,
                  messages.ERROR,
                  _(u'Service %(url)s not allowed.') % {'url': self.service}
              )
          except models.BadUsername:
              error = 2
              messages.add_message(
                  self.request,
                  messages.ERROR,
                  _(u"Username not allowed")
              )
          except models.BadFilter:
              error = 3
              messages.add_message(
                  self.request,
                  messages.ERROR,
                  _(u"User characteristics not allowed")
              )
          except models.UserFieldNotDefined:
              error = 4
              messages.add_message(
                  self.request,
                  messages.ERROR,
                  _(u"The attribute %(field)s is needed to use"
                    u" that service") % {'field': service_pattern.user_field}
              )
          if self.gateway and not self.ajax:
              list(messages.get_messages(self.request))
              return HttpResponseRedirect(self.service)
          if not self.ajax:
              return render(
                  self.request,
                  settings.CAS_LOGGED_TEMPLATE,
                  utils.context({'session': self.request.session})
              )
          else:
              data = {"status": "error", "detail": "auth", "code": error}
              return json_response(self.request, data)