Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
portal-backend
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
HBP Medical Informatics Platform
portal-backend
Commits
854466dd
Commit
854466dd
authored
9 years ago
by
Mirco Nasuti
Browse files
Options
Downloads
Patches
Plain Diff
user data
parent
3e75eabc
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/org/hbp/mip/MIPApplication.java
+26
-25
26 additions, 25 deletions
src/main/java/org/hbp/mip/MIPApplication.java
src/main/java/org/hbp/mip/model/User.java
+34
-4
34 additions, 4 deletions
src/main/java/org/hbp/mip/model/User.java
with
60 additions
and
29 deletions
src/main/java/org/hbp/mip/MIPApplication.java
+
26
−
25
View file @
854466dd
...
@@ -75,33 +75,9 @@ public class MIPApplication extends WebSecurityConfigurerAdapter {
...
@@ -75,33 +75,9 @@ public class MIPApplication extends WebSecurityConfigurerAdapter {
@RequestMapping
(
"/user"
)
@RequestMapping
(
"/user"
)
@ResponseBody
@ResponseBody
public
Principal
user
(
Principal
principal
)
{
public
Principal
user
(
Principal
principal
)
{
OAuth2Authentication
oAuth2Authentication
=
(
OAuth2Authentication
)
SecurityContextHolder
.
getContext
().
getAuthentication
();
Authentication
userAuthentication
=
oAuth2Authentication
.
getUserAuthentication
();
System
.
out
.
println
(
"##############################"
);
System
.
out
.
println
(
userAuthentication
.
getDetails
());
System
.
out
.
println
(
"##############################"
);
return
principal
;
return
principal
;
}
}
private
User
principalToUser
(
Principal
principal
)
{
Session
session
=
HibernateUtil
.
getSessionFactory
().
getCurrentSession
();
session
.
beginTransaction
();
org
.
hibernate
.
Query
query
=
session
.
createQuery
(
"from User where username= :username"
);
query
.
setString
(
"username"
,
principal
.
getName
());
User
user
=
(
User
)
query
.
uniqueResult
();
session
.
getTransaction
().
commit
();
if
(
user
==
null
)
{
session
=
HibernateUtil
.
getSessionFactory
().
getCurrentSession
();
session
.
beginTransaction
();
user
=
new
User
(
principal
);
session
.
save
(
user
);
session
.
getTransaction
().
commit
();
}
return
user
;
}
@RequestMapping
(
value
=
"/articles"
,
method
=
RequestMethod
.
GET
)
@RequestMapping
(
value
=
"/articles"
,
method
=
RequestMethod
.
GET
)
@ResponseBody
@ResponseBody
public
List
<
Article
>
getArticles
()
{
public
List
<
Article
>
getArticles
()
{
...
@@ -161,7 +137,7 @@ public class MIPApplication extends WebSecurityConfigurerAdapter {
...
@@ -161,7 +137,7 @@ public class MIPApplication extends WebSecurityConfigurerAdapter {
@RequestMapping
(
value
=
"/articles"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
value
=
"/articles"
,
method
=
RequestMethod
.
POST
)
@ResponseBody
@ResponseBody
public
Article
postArticle
(
@RequestBody
Article
article
,
Principal
principal
)
{
public
Article
postArticle
(
@RequestBody
Article
article
,
Principal
principal
)
{
User
user
=
principalTo
User
(
principal
);
User
user
=
get
User
(
principal
);
Session
session
=
HibernateUtil
.
getSessionFactory
().
getCurrentSession
();
Session
session
=
HibernateUtil
.
getSessionFactory
().
getCurrentSession
();
session
.
beginTransaction
();
session
.
beginTransaction
();
article
.
setCreatedAt
(
new
Date
());
article
.
setCreatedAt
(
new
Date
());
...
@@ -327,4 +303,29 @@ public class MIPApplication extends WebSecurityConfigurerAdapter {
...
@@ -327,4 +303,29 @@ public class MIPApplication extends WebSecurityConfigurerAdapter {
return
repository
;
return
repository
;
}
}
private
String
getUserInfos
()
{
OAuth2Authentication
oAuth2Authentication
=
(
OAuth2Authentication
)
SecurityContextHolder
.
getContext
().
getAuthentication
();
Authentication
userAuthentication
=
oAuth2Authentication
.
getUserAuthentication
();
System
.
out
.
println
(
userAuthentication
.
getDetails
().
toString
());
return
userAuthentication
.
getDetails
().
toString
();
}
private
User
getUser
(
Principal
principal
)
{
Session
session
=
HibernateUtil
.
getSessionFactory
().
getCurrentSession
();
session
.
beginTransaction
();
org
.
hibernate
.
Query
query
=
session
.
createQuery
(
"from User where username= :username"
);
query
.
setString
(
"username"
,
principal
.
getName
());
User
user
=
(
User
)
query
.
uniqueResult
();
session
.
getTransaction
().
commit
();
if
(
user
==
null
)
{
session
=
HibernateUtil
.
getSessionFactory
().
getCurrentSession
();
session
.
beginTransaction
();
user
=
new
User
(
getUserInfos
());
session
.
save
(
user
);
session
.
getTransaction
().
commit
();
}
return
user
;
}
}
}
This diff is collapsed.
Click to expand it.
src/main/java/org/hbp/mip/model/User.java
+
34
−
4
View file @
854466dd
...
@@ -5,9 +5,10 @@
...
@@ -5,9 +5,10 @@
package
org.hbp.mip.model
;
package
org.hbp.mip.model
;
import
javax.persistence.*
;
import
javax.persistence.*
;
import
java.security.Principal
;
import
java.util.LinkedList
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
@Entity
@Entity
@Table
(
name
=
"user_mip"
)
@Table
(
name
=
"user_mip"
)
...
@@ -39,10 +40,39 @@ public class User {
...
@@ -39,10 +40,39 @@ public class User {
public
User
()
{
public
User
()
{
}
}
public
User
(
P
rin
cipal
principal
)
public
User
(
St
rin
g
userInfo
)
{
{
this
.
username
=
principal
.
getName
();
Pattern
p
;
this
.
fullname
=
this
.
username
;
Matcher
m
;
p
=
Pattern
.
compile
(
"username=([\\w ]+)"
);
m
=
p
.
matcher
(
userInfo
);
if
(
m
.
find
())
{
System
.
out
.
println
(
m
.
group
(
1
));
this
.
username
=
m
.
group
(
1
);
}
p
=
Pattern
.
compile
(
"displayName=([\\w ]+)"
);
m
=
p
.
matcher
(
userInfo
);
if
(
m
.
find
())
{
System
.
out
.
println
(
m
.
group
(
1
));
this
.
fullname
=
m
.
group
(
1
);
}
p
=
Pattern
.
compile
(
"givenName=([\\w ]+)"
);
m
=
p
.
matcher
(
userInfo
);
if
(
m
.
find
())
{
System
.
out
.
println
(
m
.
group
(
1
));
this
.
firstname
=
m
.
group
(
1
);
}
p
=
Pattern
.
compile
(
"familyName=([\\w ]+)"
);
m
=
p
.
matcher
(
userInfo
);
if
(
m
.
find
())
{
System
.
out
.
println
(
m
.
group
(
1
));
this
.
lastname
=
m
.
group
(
1
);
}
}
}
public
Long
getId
()
{
public
Long
getId
()
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment