<commit_before>package se.su.it.signuptool

import javax.servlet.http.Cookie
import se.su.it.ws.commons.WSLocatorFactory
import se.su.it.sukat.client.CardInfoFacade
import ladok.lpw.service.changeaddress.facadeclient.ChangeAddressVO

class SignupController {

  def UtilityService
  def LPWWebService
  def WsMethodService

  def index = {
    def link = 'https://public.it.secure.su.se/shibboleth/Shibboleth.sso/WAYF/studera.nu/produktion?target=https://sukattool-web1.it.su.se:1043/foo/signuptool/setup'

    [link: link, hostname: System.getProperty('signuptool.hostname')]
  }

  def test = {
    Properties p = new Properties()
    p.setProperty("MailRoutingFacade", "https://sukat-svc-test.it.su.se/sukatsvc-ws/services/MailRoutingFacade")
    p.setProperty("CardInfoFacade", "https://sukat-svc.it.su.se/sukatsvc-ws/services/CardInfoFacade")
    def wsloc = WSLocatorFactory.instance(p)
    CardInfoFacade fac = wsloc.getService(CardInfoFacade.class)
    def crads = fac.getAllCards("rnyberg", true)

    ChangeAddressVO addr = LPWWebService.getChangeAddressVO('marcus')
    render "${crads} och ${addr}"
  }

  def accountSetup = {

    def attrs = new ShibAttributes() // Initiate with shib data from studera.nu

    // Validates all of the models attributes and renders map of errors if invalid
    if (!attrs.validate()) {
      flash.message = attrs.getErrorMessages()
      redirect(action: 'index')
      return
    }

    // Should we have a physto whitelist?

    // We now have valid shib data, proceed to WS calling...
    def enrollmentFacade = null
    def accountFacade = null
    def userContactFacade = null

    // reset account only if cookies for password and uid havent been set
    if (!request?.cookies['nin'] || !request?.cookies['session']) { //!defined $q->cookie('session' || $nin ne $q->cookie("nin"))
      def vo = WsMethodService?.findEnrolledUserByNIN(nin)
      if (!vo && domain =~ /student.su.se/) {
        vo = WsMethodService?.enrollUser(domain, givenName, sn, "other", nin)
      }

      if (!vo?.uid && !vo?.password) {
        flash.error = "Aktiveringen av ditt konto misslyckades."
        // do something
      }

      def isEnabled = WsMethodService?.isBasicServicesEnabled(vo.uid)
      WsMethodService?.enableBasicServices(vo.uid)
      def mail = null
      if (isEnabled) {
        mail = WsMethodService?.getMail(vo.uid)
      }

      //set name for password_cookie to something other than password, and encode base64
      def password_cookie = new Cookie('session', vo.password.encodeAsBase64())
      def uid_cookie = new Cookie('uid', vo.uid)
      def nin_cookie = new Cookie('nin', nin)

      response.addCookie(password_cookie)
      response.addCookie(uid_cookie)
      response.addCookie(nin_cookie)

      [uid: uid]
    }
  }
}<commit_msg>tidy up ws calling bit as well<commit_after>package se.su.it.signuptool

import javax.servlet.http.Cookie
import se.su.it.ws.commons.WSLocatorFactory
import se.su.it.sukat.client.CardInfoFacade
import ladok.lpw.service.changeaddress.facadeclient.ChangeAddressVO

class SignupController {

  def UtilityService
  def LPWWebService
  def WsMethodService

  def index = {
    def link = 'https://public.it.secure.su.se/shibboleth/Shibboleth.sso/WAYF/studera.nu/produktion?target=https://sukattool-web1.it.su.se:1043/foo/signuptool/setup'

    [link: link, hostname: System.getProperty('signuptool.hostname')]
  }

  def test = {
    Properties p = new Properties()
    p.setProperty("MailRoutingFacade", "https://sukat-svc-test.it.su.se/sukatsvc-ws/services/MailRoutingFacade")
    p.setProperty("CardInfoFacade", "https://sukat-svc.it.su.se/sukatsvc-ws/services/CardInfoFacade")
    def wsloc = WSLocatorFactory.instance(p)
    CardInfoFacade fac = wsloc.getService(CardInfoFacade.class)
    def crads = fac.getAllCards("rnyberg", true)

    ChangeAddressVO addr = LPWWebService.getChangeAddressVO('marcus')
    render "${crads} och ${addr}"
  }

  def accountSetup = {

    def attrs = new ShibAttributes() // Initiate with shib data from studera.nu

    // Validates all of the models attributes and renders map of errors if invalid
    if (!attrs.validate()) {
      flash.message = attrs.getErrorMessages()
      redirect(action: 'index')
      return
    }

    // Should we have a physto whitelist?

    // We now have valid shib data, proceed to WS calling...
    def enrollmentFacade = null
    def accountFacade = null
    def userContactFacade = null

    // Reset account only if cookies for password and uid havent been set
    if (!request?.cookies['session'] || request?.cookies['nin'] != attrs.nin) {

      def vo = WsMethodService?.findEnrolledUserByNIN(attrs.nin)
      if (!vo && attrs.domain =~ /student.su.se/) {
        vo = WsMethodService?.enrollUser(attrs.domain, attrs.givenName, attrs.sn, "other", attrs.nin)
      }

      if (!vo?.uid && !vo?.password) {
        flash.error = "Aktiveringen av ditt konto misslyckades."
        // do something
      }

      def isEnabled = WsMethodService?.isBasicServicesEnabled(vo.uid)
      WsMethodService?.enableBasicServices(vo.uid)
      def mail = null
      if (isEnabled) {
        mail = WsMethodService?.getMail(vo.uid)
      }

      //set name for password_cookie to something other than password, and encode base64
      def password_cookie = new Cookie('session', vo.password.encodeAsBase64())
      def uid_cookie = new Cookie('uid', vo.uid)
      def nin_cookie = new Cookie('nin', attrs.nin)

      response.addCookie(password_cookie)
      response.addCookie(uid_cookie)
      response.addCookie(nin_cookie)
    }

    [status: 'success']
  }
}
{'fraction_non_alphanumeric': 0.11765749778172138, 'fraction_numerical': 0.003194321206743567, 'mean_word_length': 3.2957317073170733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '678669', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1824, 'n_words': 398}