Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

Commit 7fe0d2d

Browse files
committed
Adding login to the database
1 parent 9d9c01a commit 7fe0d2d

File tree

10 files changed

+209
-67
lines changed

10 files changed

+209
-67
lines changed

src/main/java/pl/simplemethod/codebin/githubOauth/GithubClient.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,16 @@ protected org.json.JSONObject getCloneReposMinimalInfo(String token, String user
6969
* @param reposName Name of the repository
7070
* @return Json object with data
7171
*/
72-
protected String getReposContributorsStatistics(String token, String username, String reposName) {
72+
protected org.json.JSONObject getReposContributorsStatistics(String token, String username, String reposName) {
73+
org.json.JSONObject body = new org.json.JSONObject();
7374
try {
7475
HttpResponse<JsonNode> reposInfo = Unirest.get("https://api.github.com/repos/" + username + "/" + reposName +"/stats/contributors").header("accept", "application/json").header("Authorization", "Bearer " + token).header("Content-Type", "application/json").asJson();
75-
return reposInfo.getBody().toString();
76+
body = reposInfo.getBody().getObject();
77+
return body;
7678
} catch (UnirestException e) {
77-
return "error " + e.toString();
79+
body.put("ok", false);
80+
body.put("exception", e);
81+
return body;
7882
}
7983
}
8084

@@ -86,12 +90,16 @@ protected String getReposContributorsStatistics(String token, String username, S
8690
* @param reposName Name of the repository
8791
* @return Json object with data
8892
*/
89-
protected String getReposInfo(String token, String username, String reposName) {
93+
protected org.json.JSONObject getReposInfo(String token, String username, String reposName) {
94+
org.json.JSONObject body = new org.json.JSONObject();
9095
try {
9196
HttpResponse<JsonNode> userInfo = Unirest.get("https://api.github.com/repos/" + username + "/" + reposName).header("accept", "application/json").header("Authorization", "Bearer " + token).header("Content-Type", "application/json").asJson();
92-
return userInfo.getBody().toString();
97+
body = userInfo.getBody().getObject();
98+
return body;
9399
} catch (UnirestException e) {
94-
return "error " + e.toString();
100+
body.put("ok", false);
101+
body.put("exception", e);
102+
return body;
95103
}
96104
}
97105

@@ -101,12 +109,16 @@ protected String getReposInfo(String token, String username, String reposName) {
101109
* @param token Token for authorization
102110
* @return Json object with data
103111
*/
104-
protected String getUserRepos(String token) {
112+
protected org.json.JSONObject getUserRepos(String token) {
113+
org.json.JSONObject body = new org.json.JSONObject();
105114
try {
106115
HttpResponse<JsonNode> userReposInfo = Unirest.get("https://api.github.com/user/repos").header("accept", "application/json").header("Authorization", "Bearer " + token).header("Content-Type", "application/json").asJson();
107-
return userReposInfo.getBody().toString();
116+
body = userReposInfo.getBody().getObject();
117+
return body;
108118
} catch (UnirestException e) {
109-
return "error " + e.toString();
119+
body.put("ok", false);
120+
body.put("exception", e);
121+
return body;
110122
}
111123
}
112124

@@ -117,12 +129,16 @@ protected String getUserRepos(String token) {
117129
* @param username Username
118130
* @return Json object with data
119131
*/
120-
protected String getUserInfo(String token, String username) {
132+
protected org.json.JSONObject getUserInfo(String token, String username) {
133+
org.json.JSONObject body = new org.json.JSONObject();
121134
try {
122135
HttpResponse<JsonNode> userInfo = Unirest.get("https://api.github.com/users/" + username).header("accept", "application/json").header("Authorization", "Bearer " + token).header("Content-Type", "application/json").asJson();
123-
return userInfo.getBody().toString();
136+
body = userInfo.getBody().getObject();
137+
return body;
124138
} catch (UnirestException e) {
125-
return "error " + e.toString();
139+
body.put("ok", false);
140+
body.put("exception", e);
141+
return body;
126142
}
127143
}
128144

@@ -133,12 +149,16 @@ protected String getUserInfo(String token, String username) {
133149
* @param token Token for authorization
134150
* @return Json object with data
135151
*/
136-
protected String getUserInfo(String token) {
152+
protected org.json.JSONObject getUserInfo(String token) {
153+
org.json.JSONObject body = new org.json.JSONObject();
137154
try {
138155
HttpResponse<JsonNode> userInfo = Unirest.get("https://api.github.com/user").header("accept", "application/json").header("Authorization", "Bearer " + token).header("Content-Type", "application/json").asJson();
139-
return userInfo.getBody().toString();
156+
body = userInfo.getBody().getObject();
157+
return body;
140158
} catch (UnirestException e) {
141-
return "error " + e.toString();
159+
body.put("ok", false);
160+
body.put("exception", e);
161+
return body;
142162
}
143163
}
144164

src/main/java/pl/simplemethod/codebin/githubOauth/GithubRestController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ ResponseEntity repos(@CookieValue("token") String token) {
8888
ResponseEntity getInfoAboutOwner(@CookieValue("token") String token) {
8989
HttpHeaders headers = new HttpHeaders();
9090
headers.setContentType(MediaType.APPLICATION_JSON);
91-
return new ResponseEntity<>(githubClient.getUserInfo(token), headers, HttpStatus.valueOf(200));
91+
return new ResponseEntity<>(githubClient.getUserInfo(token).toString(), headers, HttpStatus.valueOf(200));
9292
}
9393
/**
9494
* Returns JSON with user information

src/main/java/pl/simplemethod/codebin/githubOauth/PostLogin.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,55 @@
88
import org.springframework.web.bind.annotation.RequestParam;
99
import org.springframework.web.bind.annotation.ResponseBody;
1010
import org.springframework.web.bind.annotation.RestController;
11+
import pl.simplemethod.codebin.model.Users;
12+
import pl.simplemethod.codebin.repository.UsersRepository;
1113

1214
import javax.servlet.http.Cookie;
1315
import javax.servlet.http.HttpServletResponse;
16+
import java.util.ArrayList;
17+
import java.util.List;
1418

1519
@RestController
1620
public class PostLogin {
1721

1822
@Autowired
1923
GithubClient githubClient;
2024

25+
@Autowired
26+
UsersRepository usersRepository;
27+
28+
2129
/**
2230
* Authorizes and saves a token in a cookie.
31+
*
2332
* @param response HttpServletResponse to save cookies
24-
* @param code Authorization code received from Github
33+
* @param code Authorization code received from Github
2534
* @return Code html
2635
*/
2736
@GetMapping("/postlogin")
2837
public @ResponseBody
2938
ResponseEntity postlogin(HttpServletResponse response, @RequestParam("code") String code) {
3039
HttpHeaders headers = new HttpHeaders();
31-
String token;
40+
String token, githubId;
41+
List<Users> usersList = new ArrayList<>();
3242
try {
33-
token = (String) githubClient.getAccessToken(code).get("access_token");
43+
44+
token = githubClient.getAccessToken(code).get("access_token").toString();
45+
46+
githubId = githubClient.getUserInfo(token).get("id").toString();
47+
Users users = usersRepository.getFirstByGithub(Integer.valueOf(githubId));
48+
if (users == null) {
49+
usersList.add(
50+
new Users(token, Integer.valueOf(githubId), "user", null, null)
51+
);
52+
usersList.forEach(usersRepository::save);
53+
}
54+
else
55+
{
56+
users.setToken(token);
57+
usersRepository.save(users);
58+
}
59+
3460
} catch (org.json.JSONException e) {
3561
return new ResponseEntity<>(e, headers, HttpStatus.valueOf(404));
3662
}

src/main/java/pl/simplemethod/codebin/model/Containers.java

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package pl.simplemethod.codebin.model;
22

3-
import org.springframework.lang.NonNull;
4-
53
import javax.persistence.*;
64
import java.io.Serializable;
75

@@ -15,43 +13,49 @@ public class Containers implements Serializable {
1513
@Column(name = "id")
1614
private Integer id;
1715

18-
@NonNull
19-
@Column
16+
@Column(unique = true, nullable = false)
2017
private String name;
2118

22-
@NonNull
23-
@Column(name = "id_docker")
19+
@Column(name = "id_docker", unique = true, nullable = false)
2420
private String idDocker;
2521

26-
@NonNull
2722
@OneToOne(cascade = CascadeType.ALL)
28-
@JoinColumn(name = "images_id", referencedColumnName = "id")
23+
@JoinColumn(name = "id_images", referencedColumnName = "id")
2924
private Images image;
3025

31-
@NonNull
32-
@Column(name = "exposed_ports")
26+
@Column(name = "exposed_ports", nullable = false)
3327
private Integer exposedPorts;
3428

35-
@NonNull
36-
@Column(name = "host_ports")
29+
@Column(name = "host_ports", nullable = false)
3730
private Integer hostPorts;
3831

39-
@NonNull
40-
@Column(name = "ram_memory")
32+
@Column(name = "ram_memory", nullable = false)
4133
private Long ramMemory;
4234

43-
@NonNull
44-
@Column(name = "disk_quota")
35+
@Column(name = "disk_quota", nullable = false)
4536
private Long diskQuota;
4637

47-
@NonNull
48-
@Column(name = "status")
49-
private Integer status;
38+
@Column(name = "status", nullable = false)
39+
private Integer status = 0;
5040

51-
@NonNull
52-
@Column(name = "create_time")
41+
@Column(name = "create_time", nullable = false)
5342
private Long createTime;
5443

44+
public Containers(String name, String idDocker, Images image, Integer exposedPorts, Integer hostPorts, Long ramMemory, Long diskQuota, Integer status, Long createTime) {
45+
this.name = name;
46+
this.idDocker = idDocker;
47+
this.image = image;
48+
this.exposedPorts = exposedPorts;
49+
this.hostPorts = hostPorts;
50+
this.ramMemory = ramMemory;
51+
this.diskQuota = diskQuota;
52+
this.status = status;
53+
this.createTime = createTime;
54+
}
55+
56+
public Containers() {
57+
}
58+
5559
public Integer getId() {
5660
return id;
5761
}
@@ -132,18 +136,6 @@ public void setCreateTime(Long createTime) {
132136
this.createTime = createTime;
133137
}
134138

135-
public Containers(String name, String idDocker, Images image, Integer exposedPorts, Integer hostPorts, Long ramMemory, Long diskQuota, Integer status, Long createTime) {
136-
this.name = name;
137-
this.idDocker = idDocker;
138-
this.image = image;
139-
this.exposedPorts = exposedPorts;
140-
this.hostPorts = hostPorts;
141-
this.ramMemory = ramMemory;
142-
this.diskQuota = diskQuota;
143-
this.status = status;
144-
this.createTime = createTime;
145-
}
146-
147139
@Override
148140
public String toString() {
149141
return "Containers{" +

src/main/java/pl/simplemethod/codebin/model/Images.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public class Images {
2121
@Column(name = "type")
2222
private String type;
2323

24+
public Images(String name, String type) {
25+
this.name = name;
26+
this.type = type;
27+
}
28+
29+
public Images() {
30+
}
31+
2432
public Integer getId() {
2533
return id;
2634
}
@@ -53,11 +61,4 @@ public String toString() {
5361
", type='" + type + '\'' +
5462
'}';
5563
}
56-
57-
public Images(String name, String type) {
58-
this.name = name;
59-
this.type = type;
60-
}
61-
62-
public Images(){}
6364
}
Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,101 @@
11
package pl.simplemethod.codebin.model;
22

3+
import org.springframework.lang.NonNull;
4+
35
import javax.persistence.*;
6+
import java.io.Serializable;
47
import java.util.ArrayList;
58
import java.util.List;
69

710
@Entity
811
@Table(name = "users")
9-
public class Users {
12+
public class Users implements Serializable {
13+
1014

15+
@OneToMany()
16+
List<Containers> containers = new ArrayList<>();
1117
@Id
1218
@GeneratedValue(strategy = GenerationType.IDENTITY)
1319
@Column(name = "id")
1420
private Integer id;
15-
1621
@Column(unique = true, nullable = false)
1722
private String token;
18-
1923
@Column(unique = true)
2024
private String subscription;
25+
@Column(name = "id_github", unique = true, nullable = false)
26+
private Integer github;
27+
@NonNull
28+
@Column(name = "role")
29+
private String role;
2130

22-
@OneToMany
23-
List<Containers> containers = new ArrayList<>();
31+
public Users() {
32+
}
33+
34+
public Users(String token, Integer github, String role, String subscription, List<Containers> containers) {
35+
this.containers = containers;
36+
this.token = token;
37+
this.subscription = subscription;
38+
this.github = github;
39+
this.role = role;
40+
}
41+
42+
public String getRole() {
43+
return role;
44+
}
45+
46+
public void setRole(String role) {
47+
this.role = role;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return "Users{" +
53+
"containers=" + containers +
54+
", id=" + id +
55+
", token='" + token + '\'' +
56+
", subscription='" + subscription + '\'' +
57+
", github=" + github +
58+
", role='" + role + '\'' +
59+
'}';
60+
}
61+
62+
public List<Containers> getContainers() {
63+
return containers;
64+
}
65+
66+
public void setContainers(List<Containers> containers) {
67+
this.containers = containers;
68+
}
69+
70+
public Integer getId() {
71+
return id;
72+
}
73+
74+
public void setId(Integer id) {
75+
this.id = id;
76+
}
77+
78+
public String getToken() {
79+
return token;
80+
}
81+
82+
public void setToken(String token) {
83+
this.token = token;
84+
}
85+
86+
public String getSubscription() {
87+
return subscription;
88+
}
89+
90+
public void setSubscription(String subscription) {
91+
this.subscription = subscription;
92+
}
93+
94+
public Integer getGithub() {
95+
return github;
96+
}
97+
98+
public void setGithub(Integer github) {
99+
this.github = github;
100+
}
24101
}

0 commit comments

Comments
 (0)