Search in sources :

Example 1 with User

use of custom.objects.User in project bible-online by m0ver.

the class login method initialize_user.

private void initialize_user() {
    try (DatabaseOperator operator = new DatabaseOperator()) {
        operator.createStatement(false);
        operator.execute("ALTER TABLE bible.`User` MODIFY COLUMN password varchar(33) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;\n");
    } catch (ApplicationException e) {
        e.printStackTrace();
    }
    User user = new User();
    user.setId("8cb220a6-35d0-40ec-982c-138907664454");
    user.setUsername("James");
    user.setStatus(true);
    String password = "0123456";
    user.setPassword(new Security(user.getUsername()).encode(password));
    user.setEmail("moverinfo@gmail.com");
    try {
        user.update();
    } catch (ApplicationException e) {
        e.printStackTrace();
    }
}
Also used : ApplicationException(org.tinystruct.ApplicationException) User(custom.objects.User) Security(custom.util.Security) DatabaseOperator(org.tinystruct.data.DatabaseOperator)

Example 2 with User

use of custom.objects.User in project bible-online by m0ver.

the class password method send.

public boolean send(String mailto) throws ApplicationException {
    User user = new User();
    Table table = user.findWith("WHERE email=?", new Object[] { mailto });
    if (table.size() > 0) {
        org.tinystruct.data.component.Row row = table.get(0);
        try {
            SimpleMail themail = new SimpleMail();
            themail.setFrom(this.getProperty("mail.default.from"));
            themail.setSubject("密码重置邮件");
            themail.setBody("亲爱的" + row.getFieldInfo("username").stringValue() + "用户,我们刚刚收到您的密码找回请求。为了保证您能及时使用我们提供的服务,请您于24小时内点击此链接重置您的密码。");
            themail.setTo(mailto);
            return themail.send();
        } catch (Exception ex) {
            throw new ApplicationException(ex.getMessage(), ex.getCause());
        }
    }
    return false;
}
Also used : User(custom.objects.User) Table(org.tinystruct.data.component.Table) SimpleMail(org.tinystruct.mail.SimpleMail) ApplicationException(org.tinystruct.ApplicationException) ApplicationException(org.tinystruct.ApplicationException)

Example 3 with User

use of custom.objects.User in project bible-online by m0ver.

the class login method oAuth2callback.

public String oAuth2callback() throws ApplicationException {
    HttpServletRequest request = (HttpServletRequest) this.context.getAttribute(HTTP_REQUEST);
    HttpServletResponse response = (HttpServletResponse) this.context.getAttribute(HTTP_RESPONSE);
    Reforward reforward = new Reforward(request, response);
    TokenResponse oauth2_response;
    try {
        if (this.getVariable("google_client_secrets") == null) {
            clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(login.class.getResourceAsStream("/clients_secrets.json")));
            if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
                System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/ ");
            }
            this.setVariable(new ObjectVariable("google_client_secrets", clientSecrets), false);
        } else
            clientSecrets = (GoogleClientSecrets) this.getVariable("google_client_secrets").getValue();
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, clientSecrets, SCOPES).build();
        oauth2_response = flow.newTokenRequest(request.getParameter("code")).setRedirectUri(this.getLink("oauth2callback")).execute();
        System.out.println("Ok:" + oauth2_response.toString());
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        throw new ApplicationException(e1.getMessage(), e1);
    } catch (GeneralSecurityException e) {
        // TODO Auto-generated catch block
        throw new ApplicationException(e.getMessage(), e);
    }
    try {
        HttpClient httpClient = new DefaultHttpClient();
        String url = "https://www.google.com/m8/feeds/contacts/default/full";
        url = "https://www.googleapis.com/oauth2/v1/userinfo";
        HttpGet httpget = new HttpGet(url + "?access_token=" + oauth2_response.getAccessToken());
        httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8");
        HttpResponse http_response = httpClient.execute(httpget);
        HeaderIterator iterator = http_response.headerIterator();
        while (iterator.hasNext()) {
            Header next = iterator.nextHeader();
            System.out.println(next.getName() + ":" + next.getValue());
        }
        com.google.api.client.http.HttpTransport h;
        InputStream instream = http_response.getEntity().getContent();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len;
        while ((len = instream.read(bytes)) != -1) {
            out.write(bytes, 0, len);
        }
        instream.close();
        out.close();
        Struct struct = new Builder();
        struct.parse(new String(out.toByteArray(), "utf-8"));
        this.usr = new User();
        this.usr.setEmail(struct.toData().getFieldInfo("email").stringValue());
        if (this.usr.findOneByKey("email", this.usr.getEmail()).size() == 0) {
            usr.setPassword("");
            usr.setUsername(usr.getEmail());
            usr.setLastloginIP(request.getRemoteAddr());
            usr.setLastloginTime(new Date());
            usr.setRegistrationTime(new Date());
            usr.append();
        }
        new passport(request, response, "waslogined").setLoginAsUser(this.usr.getId());
        reforward.setDefault(URLDecoder.decode(this.getVariable("from").getValue().toString(), "utf8"));
        reforward.forward();
        return new String(out.toByteArray(), "utf-8");
    } catch (ClientProtocolException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (ParseException e) {
        throw new ApplicationException(e.getMessage(), e);
    }
}
Also used : User(custom.objects.User) HttpGet(org.apache.http.client.methods.HttpGet) Builder(org.tinystruct.data.component.Builder) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Struct(org.tinystruct.data.component.Struct) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectVariable(org.tinystruct.system.template.variable.ObjectVariable) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) GeneralSecurityException(java.security.GeneralSecurityException) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpResponse(org.apache.http.HttpResponse) Date(java.util.Date) ApplicationException(org.tinystruct.ApplicationException) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) Header(org.apache.http.Header) Reforward(org.tinystruct.handler.Reforward) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HeaderIterator(org.apache.http.HeaderIterator) ParseException(org.apache.http.ParseException)

Example 4 with User

use of custom.objects.User in project bible-online by m0ver.

the class login method oAuth2_github_callback.

public String oAuth2_github_callback() throws ApplicationException {
    HttpServletRequest request = (HttpServletRequest) this.context.getAttribute(HTTP_REQUEST);
    HttpServletResponse response = (HttpServletResponse) this.context.getAttribute(HTTP_RESPONSE);
    Reforward reforward = new Reforward(request, response);
    if (this.getVariable("github_client_secrets") == null) {
        TextFileLoader loader = new TextFileLoader();
        loader.setInputStream(login.class.getResourceAsStream("/clients_secrets.json"));
        builder = new Builder();
        builder.parse(loader.getContent().toString());
        if (builder.get("github") instanceof Builder) {
            builder = (Builder) builder.get("github");
            System.out.println(builder.get("client_secret"));
            System.out.println(builder.get("client_id"));
            this.setVariable(new ObjectVariable("github_client_secrets", builder), false);
        }
    } else
        builder = (Builder) this.getVariable("github_client_secrets").getValue();
    String arguments = this.http_client("https://github.com/login/oauth/access_token?client_id=" + builder.get("client_id") + "&client_secret=" + builder.get("client_secret") + "&code=" + request.getParameter("code"));
    try {
        HttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.github.com/user";
        HttpGet httpget = new HttpGet(url + "?" + arguments);
        httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8");
        HttpResponse http_response = httpClient.execute(httpget);
        HeaderIterator iterator = http_response.headerIterator();
        while (iterator.hasNext()) {
            Header next = iterator.nextHeader();
            System.out.println(next.getName() + ":" + next.getValue());
        }
        InputStream instream = http_response.getEntity().getContent();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len;
        while ((len = instream.read(bytes)) != -1) {
            out.write(bytes, 0, len);
        }
        instream.close();
        out.close();
        Struct struct = new Builder();
        struct.parse(new String(out.toByteArray(), "utf-8"));
        this.usr = new User();
        this.usr.setEmail(struct.toData().getFieldInfo("email").stringValue());
        if (this.usr.findOneByKey("email", this.usr.getEmail()).size() == 0) {
            usr.setPassword("");
            usr.setUsername(usr.getEmail());
            usr.setLastloginIP(request.getRemoteAddr());
            usr.setLastloginTime(new Date());
            usr.setRegistrationTime(new Date());
            usr.append();
        }
        new passport(request, response, "waslogined").setLoginAsUser(this.usr.getId());
        reforward.setDefault(URLDecoder.decode(this.getVariable("from").getValue().toString(), "utf8"));
        reforward.forward();
        return new String(out.toByteArray(), "utf-8");
    } catch (ClientProtocolException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (ParseException e) {
        throw new ApplicationException(e.getMessage(), e);
    }
}
Also used : User(custom.objects.User) Builder(org.tinystruct.data.component.Builder) HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) TextFileLoader(org.tinystruct.system.util.TextFileLoader) Struct(org.tinystruct.data.component.Struct) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectVariable(org.tinystruct.system.template.variable.ObjectVariable) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpResponse(org.apache.http.HttpResponse) Date(java.util.Date) ApplicationException(org.tinystruct.ApplicationException) Header(org.apache.http.Header) Reforward(org.tinystruct.handler.Reforward) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HeaderIterator(org.apache.http.HeaderIterator) ParseException(org.apache.http.ParseException)

Example 5 with User

use of custom.objects.User in project bible-online by m0ver.

the class register method append.

public boolean append() throws ApplicationException {
    Cookie cookie = this.getCookieByName("key");
    if (cookie == null) {
        throw new ApplicationException(this.getProperty("register.status"));
    }
    ActivationKey key = new ActivationKey();
    String number = cookie.getValue();
    serial serial = new serial();
    Table t = serial.findWith("WHERE number like ?", new Object[] { number });
    if (t.size() > 0) {
        throw new ApplicationException(this.getProperty("register.code.used"));
    }
    try {
        if (key.expired(number)) {
            throw new ApplicationException(this.getProperty("register.code.expired"));
        }
    } catch (ApplicationException e) {
        // TODO Auto-generated catch block
        throw new ApplicationException(this.getProperty("register.code.expired"));
    }
    if (this.request.getParameter("nickname") == null || this.request.getParameter("nickname").trim().length() == 0) {
        throw new ApplicationException(this.getProperty("register.invalid.nickname"));
    } else {
        this.user = new User();
        this.user.setNickname(this.request.getParameter("nickname"));
        this.user.setUsername(this.user.getNickname());
        this.setVariable("nickname", this.user.getNickname());
        this.session = this.request.getSession();
        this.session.setAttribute("usr", this.user);
    }
    if (this.request.getParameter("email") == null || this.request.getParameter("email").trim().length() == 0) {
        throw new ApplicationException(this.getProperty("register.invalid.email"));
    } else {
        this.user.setEmail(this.request.getParameter("email"));
        this.setVariable("email", this.user.getEmail());
        this.session.setAttribute("usr", this.user);
    }
    if (this.request.getParameter("password") == null || this.request.getParameter("password").trim().length() == 0) {
        throw new ApplicationException(this.getProperty("register.invalid.password"));
    } else {
        this.user.setPassword(this.request.getParameter("password"));
        this.session.setAttribute("usr", this.user);
    }
    if (user.setRequestFields("count(*) as p").findWith("WHERE email=?", new Object[] { this.user.getEmail() }).get(0).getFieldInfo("p").intValue() == 0) {
        user.setPassword(new Security(user.getEmail()).encodePassword(this.user.getPassword()));
        user.setUsername(this.user.getEmail());
        user.setLastloginIP(this.request.getRemoteAddr());
        user.setLastloginTime(new Date());
        user.setRegistrationTime(new Date());
        if (this.request.getParameter("last-name") == null || this.request.getParameter("last-name").trim().length() == 0 || this.request.getParameter("first-name") == null || this.request.getParameter("first-name").trim().length() == 0) {
            throw new ApplicationException(this.getProperty("register.invalid.gender"));
        } else {
            this.user.setLastName(this.request.getParameter("last-name"));
            this.user.setFirstName(this.request.getParameter("first-name"));
            this.setVariable("lastname", this.user.getLastName());
            this.setVariable("firstname", this.user.getFirstName());
            this.session.setAttribute("usr", this.user);
        }
        if (this.request.getParameter("gender") == null || this.request.getParameter("gender").trim().length() == 0) {
            throw new ApplicationException(this.getProperty("register.invalid.gender"));
        } else {
            this.user.setGender(Integer.parseInt(this.request.getParameter("gender")));
            switch(this.user.getGender()) {
                case 0:
                    this.setVariable("gender.male", "checked");
                    break;
                case 1:
                    this.setVariable("gender.female", "checked");
                    break;
                case 2:
                    this.setVariable("gender.security", "checked");
                    break;
                default:
                    break;
            }
            this.session.setAttribute("usr", this.user);
        }
        if (this.request.getParameter("country") == null || this.request.getParameter("country").trim().length() == 0 || this.request.getParameter("city") == null || this.request.getParameter("city").trim().length() == 0) {
            throw new ApplicationException(this.getProperty("register.invalid.country"));
        } else {
            this.user.setCountry(this.request.getParameter("country"));
            this.user.setCity(this.request.getParameter("city"));
            this.setVariable("city", this.user.getCity());
            this.setVariable("postcode", this.user.getPostcode());
            this.session.setAttribute("usr", this.user);
        }
        if (this.request.getParameter("zip-postal-code") == null || this.request.getParameter("zip-postal-code").trim().length() == 0) {
            throw new ApplicationException(this.getProperty("register.invalid.postcode"));
        } else {
            this.user.setPostcode(this.request.getParameter("zip-postal-code"));
            this.setVariable("telephone", this.user.getTelephone());
            this.session.setAttribute("usr", this.user);
        }
        user.append();
        Member member = new Member();
        member.setUserId(user.getId());
        member.setGroupId("386e27c2-5db6-4f63-b28d-68a4adec2fd6");
        member.append();
        serial.setUserId(user.getId());
        serial.setNumber(number);
        serial.append();
        return true;
    } else {
        throw new ApplicationException(this.getProperty("register.email.used"));
    }
}
Also used : Cookie(javax.servlet.http.Cookie) ApplicationException(org.tinystruct.ApplicationException) Table(org.tinystruct.data.component.Table) User(custom.objects.User) custom.objects.serial(custom.objects.serial) Security(custom.util.Security) ActivationKey(custom.util.ActivationKey) Member(custom.objects.Member) Date(java.util.Date)

Aggregations

User (custom.objects.User)5 ApplicationException (org.tinystruct.ApplicationException)5 Date (java.util.Date)3 Security (custom.util.Security)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Header (org.apache.http.Header)2 HeaderIterator (org.apache.http.HeaderIterator)2 HttpResponse (org.apache.http.HttpResponse)2 ParseException (org.apache.http.ParseException)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 HttpClient (org.apache.http.client.HttpClient)2 HttpGet (org.apache.http.client.methods.HttpGet)2 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)2 Builder (org.tinystruct.data.component.Builder)2 Struct (org.tinystruct.data.component.Struct)2 Table (org.tinystruct.data.component.Table)2 Reforward (org.tinystruct.handler.Reforward)2 ObjectVariable (org.tinystruct.system.template.variable.ObjectVariable)2 TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)1