1 | |
package liquibase.precondition.core; |
2 | |
|
3 | |
import liquibase.changelog.DatabaseChangeLog; |
4 | |
import liquibase.changelog.ChangeSet; |
5 | |
import liquibase.database.Database; |
6 | |
import liquibase.exception.PreconditionErrorException; |
7 | |
import liquibase.exception.PreconditionFailedException; |
8 | |
import liquibase.exception.ValidationErrors; |
9 | |
import liquibase.exception.Warnings; |
10 | |
import liquibase.precondition.Precondition; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
public class RunningAsPrecondition implements Precondition { |
16 | |
|
17 | |
private String username; |
18 | |
|
19 | 9 | public RunningAsPrecondition() { |
20 | 9 | username = ""; |
21 | 9 | } |
22 | |
|
23 | |
public void setUsername(String aUserName) { |
24 | 1 | username = aUserName; |
25 | 1 | } |
26 | |
|
27 | |
public String getUsername() { |
28 | 0 | return username; |
29 | |
} |
30 | |
|
31 | |
public Warnings warn(Database database) { |
32 | 0 | return new Warnings(); |
33 | |
} |
34 | |
|
35 | |
public ValidationErrors validate(Database database) { |
36 | 0 | return new ValidationErrors(); |
37 | |
} |
38 | |
|
39 | |
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) |
40 | |
throws PreconditionFailedException, PreconditionErrorException { |
41 | 0 | String loggedusername = database.getConnection().getConnectionUserName(); |
42 | 0 | if (loggedusername.indexOf('@') >= 0) { |
43 | 0 | loggedusername = loggedusername.substring(0, loggedusername.indexOf('@')); |
44 | |
} |
45 | 0 | if (!username.equalsIgnoreCase(loggedusername)) { |
46 | 0 | throw new PreconditionFailedException("RunningAs Precondition failed: expected " + username + ", was " |
47 | |
+ loggedusername, changeLog, this); |
48 | |
} |
49 | 0 | } |
50 | |
|
51 | |
public String getName() { |
52 | 9 | return "runningAs"; |
53 | |
} |
54 | |
|
55 | |
} |