-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #81 implementation of legacy permission system
- Loading branch information
1 parent
e667b22
commit bed308d
Showing
5 changed files
with
106 additions
and
12 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/com/faforever/api/data/domain/LegacyAccessLevel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.faforever.api.data.domain; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.security.core.GrantedAuthority; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Deprecated | ||
public enum LegacyAccessLevel implements GrantedAuthority { | ||
ROLE_USER(0), | ||
ROLE_MODERATOR(1), | ||
ROLE_ADMINISTRATOR(2); | ||
|
||
@Override | ||
public String getAuthority() { | ||
return this.name(); | ||
} | ||
|
||
private int code; | ||
|
||
public static LegacyAccessLevel fromCode(int code) { | ||
for (LegacyAccessLevel level : LegacyAccessLevel.values()) { | ||
if (level.code == code) { | ||
return level; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException(String.format("Code '%s' is unknown", code)); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/faforever/api/data/domain/LobbyGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.faforever.api.data.domain; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "lobby_group") | ||
@Setter | ||
@Deprecated | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class LobbyGroup { | ||
private int userId; | ||
private int accessLevel; | ||
|
||
@Column(name = "\"group\"") | ||
public int getAccessLevel() { | ||
return accessLevel; | ||
} | ||
|
||
@Id | ||
@Column(name = "user_id") | ||
public int getUserId() { | ||
return userId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 19 additions & 8 deletions
27
src/main/java/com/faforever/api/security/FafUserDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,43 @@ | ||
package com.faforever.api.security; | ||
|
||
import com.faforever.api.data.domain.LegacyAccessLevel; | ||
import com.faforever.api.data.domain.User; | ||
import lombok.Getter; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
|
||
import java.util.Collection; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
@Getter | ||
public class FafUserDetails extends org.springframework.security.core.userdetails.User { | ||
|
||
private final int id; | ||
|
||
public FafUserDetails(User user) { | ||
// TODO implement lobby_admin #81 | ||
this(user.getId(), user.getLogin(), user.getPassword(), !user.isGlobalBanned(), singletonList(new SimpleGrantedAuthority("ROLE_USER"))); | ||
public FafUserDetails(User user, Collection<? extends GrantedAuthority> authorities) { | ||
this(user.getId(), user.getLogin(), user.getPassword(), !user.isGlobalBanned(), authorities); | ||
} | ||
|
||
public FafUserDetails(int id, String username, String password, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) { | ||
super(username, password, true, true, true, accountNonLocked, authorities); | ||
this.id = id; | ||
} | ||
|
||
|
||
public boolean hasPermission(String permission) { | ||
// TODO: implement permission system #81 | ||
Collection<GrantedAuthority> authorities = this.getAuthorities(); | ||
|
||
if (authorities.contains(LegacyAccessLevel.ROLE_ADMINISTRATOR)) { | ||
return true; | ||
} | ||
|
||
if (authorities.contains(LegacyAccessLevel.ROLE_MODERATOR)) { | ||
switch (permission) { | ||
// add non-moderator permissions here | ||
case "ANY-ADMIN-ONLY-PERMISSION": | ||
return false; | ||
default: | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters