Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #762 and #763 - Last login time of an user added #772

Merged
merged 5 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Column } from 'primereact/column';
import { Button } from 'primereact/button';
import { settingsActions } from '../../../../redux/ducks/settings';
import DeleteDialog from '../dialogs/DeleteDialog';
import dateformat from 'dateformat';

/**
* Fetches and lists all users.
Expand All @@ -30,6 +31,11 @@ class UserDataTable extends React.Component {
<DataTable value={users} globalFilter={filterValue} scrollable={true} scrollHeight={maxHeight}>
<Column field="id" header="ID" />
<Column field="username" header="Username" />
<Column
field="lastLoginTime"
header="Last Login Time"
body={(user) => (user.lastLoginTime === 0 ? '-' : dateformat(user.lastLoginTime, 'yyyy-mm-dd HH:MM:ss'))}
/>
<Column field="ldapUser" header="LDAP User" body={(data) => <i className={data.ldapUser ? 'pi pi-check' : 'pi pi-times'}></i>} />
<Column
style={{ width: '3.5rem' }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import rocks.inspectit.ocelot.user.UserPermissions;
import rocks.inspectit.ocelot.user.UserService;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -58,8 +59,15 @@ public class AccountController extends AbstractBaseController {
@ApiResponse(code = 200, message = "The access token", examples =
@Example(value = @ExampleProperty(value = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTU2MTYyODE1NH0.KelDW1OXg9xlMjSiblwZqui7sya4Crq833b-98p8UZ4", mediaType = "text/plain")))
@GetMapping("account/token")
public String acuireNewAccessToken(Authentication user) {
return tokenManager.createToken(user.getName());
public String acquireNewAccessToken(Authentication auth) {
Optional<User> userOptional = userService.getUserByName(auth.getName());

if (userOptional.isPresent()) {
User user = userOptional.get();
user.setLastLoginTime(System.currentTimeMillis());
userService.addOrUpdateUser(user);
}
return tokenManager.createToken(auth.getName());
}

@ApiOperation(value = "Change Password", notes = "Changes the password of the logged in user." +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public class User implements Auditable {
@Column(nullable = false)
private boolean isLdapUser;

/**
* Indicates the time when the user last logged in.
*/
@Column
private long lastLoginTime;

@Override
@JsonIgnore
public AuditDetail getAuditDetail() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE user
ADD last_login_time BIGINT;
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,35 @@ void testAdminPermissions() {
.build());
}
}

@Nested
class AcquireNewAccessToken {

@Test
void acquireToken() {
long now = System.currentTimeMillis();

userService.addOrUpdateUser(User.builder()
.username("John")
.password("doe")
.build());

long loginTimeBefore = userService
.getUserByName("John").get()
.getLastLoginTime();
assertThat(loginTimeBefore).isZero();

ResponseEntity<String> result = rest
.withBasicAuth("John", "doe")
.getForEntity("/api/v1/account/token", String.class);

assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);

long loginTimeAfter = userService
.getUserByName("John").get()
.getLastLoginTime();
assertThat(loginTimeAfter).isGreaterThanOrEqualTo(now);
}

}
}