Commit bf13678e by Tuukka Kivilahti

Merge branch 'feature-permissions' into 'master'

Feature permissions

See merge request !418
2 parents 922271b6 2f68d903
package fi.codecrew.moya.beans;
import fi.codecrew.moya.model.EventRoleFeature;
import fi.codecrew.moya.model.MoyaFeature;
public class FeaturePermission {
private final MoyaFeature feature;
private boolean admin = false;
private boolean user = false;
private boolean info = false;
public FeaturePermission(MoyaFeature f) {
this.feature = f;
}
public void addPermissions(EventRoleFeature erf) {
user = user || erf.isUser();
info = info || erf.isInfo();
admin = admin || erf.isAdmin();
}
public MoyaFeature getFeature() {
return feature;
}
public boolean isAdmin() {
return admin;
}
public void setAdmin(boolean admin) {
this.admin = admin;
}
public boolean isUser() {
return user;
}
public void setUser(boolean user) {
this.user = user;
}
public boolean isInfo() {
return info;
}
public void setInfo(boolean info) {
this.info = info;
}
}
...@@ -27,17 +27,7 @@ import java.util.Map; ...@@ -27,17 +27,7 @@ import java.util.Map;
import javax.ejb.Local; import javax.ejb.Local;
import fi.codecrew.moya.entitysearch.UserSearchQuery; import fi.codecrew.moya.entitysearch.UserSearchQuery;
import fi.codecrew.moya.model.EventUser; import fi.codecrew.moya.model.*;
import fi.codecrew.moya.model.Feedback;
import fi.codecrew.moya.model.GameID;
import fi.codecrew.moya.model.GroupMembership;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.PrintedCard;
import fi.codecrew.moya.model.Role;
import fi.codecrew.moya.model.TournamentGame;
import fi.codecrew.moya.model.User;
import fi.codecrew.moya.model.UserApproval;
import fi.codecrew.moya.model.UserImage;
import fi.codecrew.moya.util.MailMessage; import fi.codecrew.moya.util.MailMessage;
import fi.codecrew.moya.utilities.SearchQuery; import fi.codecrew.moya.utilities.SearchQuery;
import fi.codecrew.moya.utilities.SearchResult; import fi.codecrew.moya.utilities.SearchResult;
...@@ -222,4 +212,5 @@ public interface UserBeanLocal { ...@@ -222,4 +212,5 @@ public interface UserBeanLocal {
Map<Role,List<String>> findUsersRolesWithReason(EventUser user); Map<Role,List<String>> findUsersRolesWithReason(EventUser user);
Map<MoyaFeature, FeaturePermission> getUsersFeaturepermissions(EventUser source);
} }
...@@ -631,8 +631,16 @@ public class BootstrapBean implements BootstrapBeanLocal { ...@@ -631,8 +631,16 @@ public class BootstrapBean implements BootstrapBeanLocal {
"ALTER TABLE users DROP COLUMN postal_town;" "ALTER TABLE users DROP COLUMN postal_town;"
}); });
} dbUpdates.add(new String[]{
"CREATE TABLE event_features (id SERIAL NOT NULL, created TIMESTAMPTZ, feature TEXT NOT NULL, meta jsonb, event_id SERIAL NOT NULL, PRIMARY KEY (id))",
"CREATE TABLE event_role_features (id SERIAL NOT NULL, admin_permission BOOLEAN NOT NULL, created TIMESTAMPTZ, feature TEXT NOT NULL, info_permission BOOLEAN NOT NULL, meta jsonb, user_permission BOOLEAN NOT NULL, role_id SERIAL NOT NULL, PRIMARY KEY (id))",
"ALTER TABLE event_features ADD CONSTRAINT UNQ_event_features_0 UNIQUE (event_id, feature)",
"ALTER TABLE event_role_features ADD CONSTRAINT UNQ_event_role_features_0 UNIQUE (role_id, feature)",
"ALTER TABLE event_features ADD CONSTRAINT FK_event_features_event_id FOREIGN KEY (event_id) REFERENCES events (id)",
"ALTER TABLE event_role_features ADD CONSTRAINT FK_event_role_features_role_id FOREIGN KEY (role_id) REFERENCES roles (id)"
});
}
public BootstrapBean() { public BootstrapBean() {
} }
......
...@@ -43,6 +43,7 @@ import javax.imageio.ImageIO; ...@@ -43,6 +43,7 @@ import javax.imageio.ImageIO;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import fi.codecrew.moya.model.*;
import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -68,22 +69,6 @@ import fi.codecrew.moya.facade.RoleFacade; ...@@ -68,22 +69,6 @@ import fi.codecrew.moya.facade.RoleFacade;
import fi.codecrew.moya.facade.UserApprovalFacade; import fi.codecrew.moya.facade.UserApprovalFacade;
import fi.codecrew.moya.facade.UserFacade; import fi.codecrew.moya.facade.UserFacade;
import fi.codecrew.moya.facade.UserImageFacade; import fi.codecrew.moya.facade.UserImageFacade;
import fi.codecrew.moya.model.AccountEvent;
import fi.codecrew.moya.model.Approval;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Feedback;
import fi.codecrew.moya.model.GameID;
import fi.codecrew.moya.model.GroupMembership;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.LanEventPropertyKey;
import fi.codecrew.moya.model.PlaceGroup;
import fi.codecrew.moya.model.PrintedCard;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.model.Role;
import fi.codecrew.moya.model.TournamentGame;
import fi.codecrew.moya.model.User;
import fi.codecrew.moya.model.UserApproval;
import fi.codecrew.moya.model.UserImage;
import fi.codecrew.moya.util.MailMessage; import fi.codecrew.moya.util.MailMessage;
import fi.codecrew.moya.utilities.I18n; import fi.codecrew.moya.utilities.I18n;
import fi.codecrew.moya.utilities.PasswordFunctions; import fi.codecrew.moya.utilities.PasswordFunctions;
...@@ -241,6 +226,16 @@ public class UserBean implements UserBeanLocal { ...@@ -241,6 +226,16 @@ public class UserBean implements UserBeanLocal {
} }
@Override
public Map<MoyaFeature, FeaturePermission> getUsersFeaturepermissions(EventUser user) {
EnumMap<MoyaFeature, FeaturePermission> ret = new EnumMap<>(MoyaFeature.class);
for(MoyaFeature f: MoyaFeature.values()){
ret.put(f, new FeaturePermission(f) );
}
findUsersRoles(user).forEach(role -> role.getFeatures().forEach(erf -> ret.get(erf.getFeature()).addPermissions(erf)));
return ret;
}
// private EventUser currentEventuser; // private EventUser currentEventuser;
// private ArrayList<Role> currentEventuserRoles; // private ArrayList<Role> currentEventuserRoles;
......
package fi.codecrew.moya.model;
import com.sun.istack.Nullable;
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
@Table(name = "event_features", uniqueConstraints = {@UniqueConstraint(columnNames = {EventFeature.EVENT_ID_COLUMN, EventFeature.FEATURE_COLUMN})})
public class EventFeature extends GenericEntity {
public static final String EVENT_ID_COLUMN = "event_id";
public static final String FEATURE_COLUMN = "feature";
@Column(name = "created", updatable = false)
private LocalDateTime created = LocalDateTime.now();
@ManyToOne(optional = false)
@JoinColumn(name = EVENT_ID_COLUMN, nullable = false)
private LanEvent event;
@Enumerated(EnumType.STRING)
@Column(name = FEATURE_COLUMN, nullable = false)
private MoyaFeature feature;
public LanEvent getEvent() {
return event;
}
public void setEvent(LanEvent event) {
this.event = event;
}
public MoyaFeature getFeature() {
return feature;
}
public void setFeature(MoyaFeature feature) {
this.feature = feature;
}
public LocalDateTime getCreated() {
return created;
}
public void setCreated(LocalDateTime created) {
this.created = created;
}
}
package fi.codecrew.moya.model;
import javax.persistence.*;
import java.time.LocalDateTime;
@Table(name = "event_role_features", uniqueConstraints = {@UniqueConstraint(columnNames = {EventRoleFeature.ROLE_ID_COLUMN, EventRoleFeature.FEATURE_COLUMN})})
@Entity()
public class EventRoleFeature extends GenericEntity {
public static final String ROLE_ID_COLUMN = "role_id";
public static final String FEATURE_COLUMN = "feature";
@Column(name = "created", updatable = false)
private LocalDateTime created = LocalDateTime.now();
@ManyToOne(optional = false)
@JoinColumn(name = ROLE_ID_COLUMN, nullable = false, updatable = false)
private Role role;
@Enumerated(EnumType.STRING)
@Column(name = FEATURE_COLUMN, nullable = false, updatable = false)
private MoyaFeature feature;
@Column(nullable = false, name = "user_permission")
private boolean user = false;
@Column(nullable = false, name = "info_permission")
private boolean info = false;
@Column(nullable = false, name = "admin_permission")
private boolean admin = false;
public LocalDateTime getCreated() {
return created;
}
public void setCreated(LocalDateTime created) {
this.created = created;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public MoyaFeature getFeature() {
return feature;
}
public void setFeature(MoyaFeature feature) {
this.feature = feature;
}
public boolean isUser() {
return user;
}
public void setUser(boolean user) {
this.user = user;
}
public boolean isInfo() {
return info;
}
public void setInfo(boolean info) {
this.info = info;
}
public boolean isAdmin() {
return admin;
}
public void setAdmin(boolean admin) {
this.admin = admin;
}
}
...@@ -143,6 +143,10 @@ public class LanEvent extends GenericEntity { ...@@ -143,6 +143,10 @@ public class LanEvent extends GenericEntity {
@Column(name="codecrew_notes") @Column(name="codecrew_notes")
private String codecrewNotes; private String codecrewNotes;
@OneToMany(mappedBy = "event")
@PrivateOwned
private List<EventFeature> features = new ArrayList<>();
// @OneToMany(mappedBy = "event") // @OneToMany(mappedBy = "event")
// private List<Vip> vips; // private List<Vip> vips;
...@@ -388,7 +392,15 @@ public class LanEvent extends GenericEntity { ...@@ -388,7 +392,15 @@ public class LanEvent extends GenericEntity {
return getDomains().get(0).getDomain(); return getDomains().get(0).getDomain();
} }
// public List<Vip> getVips() { public List<EventFeature> getFeatures() {
return features;
}
public void setFeatures(List<EventFeature> features) {
this.features = features;
}
// public List<Vip> getVips() {
// return vips; // return vips;
// } // }
// //
......
package fi.codecrew.moya.model;
public enum MoyaFeature {
/**
* - Users have the permission to manage themselves
* - organizers have the permission to view all users
* - Admin has the rights to modify all users basic information
* <p>
* Note that nobody should have the right to reset a password or email
* because of security concerns. (except superadmin, maybe?)
*/
USER,
/**
* - User: No permissions
* - Org: View and give out vip list enries
* - Admin: Add vips
*/
VIPLIST,
/**
* - User: View and buy items in shop
* - Org: Sell products to others.
* - Admin: Create and modify products
*/
SHOP,
/**
* - User: View and reserve places from the map to themselves
* - Org: View all information about places, and assign people to places
* - Admin: Create and modify maps and layouts
*/
MAPS,
/**
* - Guest: Vote and submit entries to compos
* - Org: View compos and assist users in entry submissions
* - Admin: Create and manage categories, view results
*/
COMPOS,
ORGROLES,
TOURNAMENTS,
/**
* - User: View and enroll to lectures
* - Org: Enroll others to lectures and view all registrants
* - Admin: Create and modify lectures, remove enrollments
*/
COURSES,
/**
* - Guest: Answer the polls
* - Organizer: No special permissions
* - Admin: Create and manage polls, View results
*/
POLLS,
FOODWAVES,
BBADGES,
/**
* Most events have some content added on the pages
* - User: View content
* - Org: No special permissions
* - Admin: Manage and create content
*/
SITE
}
...@@ -104,7 +104,10 @@ public class Role extends GenericEntity { ...@@ -104,7 +104,10 @@ public class Role extends GenericEntity {
@ManyToMany(mappedBy = "openForRoles") @ManyToMany(mappedBy = "openForRoles")
private List<Lecture> lectures = new ArrayList<Lecture>(); private List<Lecture> lectures = new ArrayList<Lecture>();
@OneToMany(mappedBy="role")
@PrivateOwned
private List<EventRoleFeature> features = new ArrayList<>();
public Role() { public Role() {
super(); super();
...@@ -247,4 +250,11 @@ public class Role extends GenericEntity { ...@@ -247,4 +250,11 @@ public class Role extends GenericEntity {
this.userSelectableRole = user_selectable_group; this.userSelectableRole = user_selectable_group;
} }
public List<EventRoleFeature> getFeatures() {
return features;
}
public void setFeatures(List<EventRoleFeature> features) {
this.features = features;
}
} }
package fi.codecrew.moya.model.converters;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.sql.Date;
@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate locDate) {
return (locDate == null ? null : Date.valueOf(locDate));
}
@Override
public LocalDate convertToEntityAttribute(Date sqlDate) {
return (sqlDate == null ? null : sqlDate.toLocalDate());
}
}
\ No newline at end of file
package fi.codecrew.moya.model.converters;
import javax.persistence.AttributeConverter;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import javax.persistence.Converter;
@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime locDate) {
return (locDate == null ? null : Timestamp.valueOf(locDate));
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
return (sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime());
}
}
\ No newline at end of file
...@@ -133,16 +133,17 @@ public class MoyaGraphQLServlet extends HttpServlet { ...@@ -133,16 +133,17 @@ public class MoyaGraphQLServlet extends HttpServlet {
String query = request.getParameter("query"); String query = request.getParameter("query");
if(query == null) { if (query == null) {
String q = request.getReader().lines().collect(Collectors.joining()); String q = request.getReader().lines().collect(Collectors.joining());
Map<String, Object> queryJson = new Gson() Map<String, Object> queryJson = new Gson()
.fromJson( .fromJson(
q, q,
new TypeToken<Map<String, Object>>(){} new TypeToken<Map<String, Object>>() {
.getType()); }
.getType());
query = queryJson.get("query").toString(); query = queryJson.get("query").toString();
//Map varJson = new Gson().fromJson(queryJson.get("variables"), Map.class); //Map varJson = new Gson().fromJson(queryJson.get("variables"), Map.class);
...@@ -261,19 +262,19 @@ public class MoyaGraphQLServlet extends HttpServlet { ...@@ -261,19 +262,19 @@ public class MoyaGraphQLServlet extends HttpServlet {
return stringWriter.toString(); return stringWriter.toString();
}).description("Returns the object as a string."); }).description("Returns the object as a string.");
b.addField(Map.Entry.class) b.addField(Map.Entry.class)
.argument(newArgument().name("keys").defaultValue(Collections.emptyMap()).type(GraphQLList.list(GraphQLString))) .argument(newArgument().name("keys").defaultValue(Collections.emptyMap()).type(GraphQLList.list(GraphQLString)))
.name("path") .name("path")
.description("Returngs values from given keys as key, value pairs") .description("Returngs values from given keys as key, value pairs")
.dataFetcher(environment -> { .dataFetcher(environment -> {
Map<String, String> ret = new HashMap<>(); Map<String, String> ret = new HashMap<>();
final JsonObject jsonObj = environment.getSource(); final JsonObject jsonObj = environment.getSource();
((List<String>) environment.getArgument("keys")).forEach(key -> { ((List<String>) environment.getArgument("keys")).forEach(key -> {
if (jsonObj.containsKey(key)) ret.put(key, jsonObj.getString(key)); if (jsonObj.containsKey(key)) ret.put(key, jsonObj.getString(key));
});
return ret.entrySet();
}); });
return ret.entrySet();
});
} }
{ {
EntityGQLBuilder<Role> b = builder.createEntity(Role.class); EntityGQLBuilder<Role> b = builder.createEntity(Role.class);
...@@ -319,7 +320,14 @@ public class MoyaGraphQLServlet extends HttpServlet { ...@@ -319,7 +320,14 @@ public class MoyaGraphQLServlet extends HttpServlet {
b.addField(Place_.currentUser); b.addField(Place_.currentUser);
b.addField(Place_.meta); b.addField(Place_.meta);
} }
{
EntityGQLBuilder<FeaturePermission> b = builder.createEntity(FeaturePermission.class);
b.addField().type(GraphQLString).name("feature").dataFetcher(environment -> ((FeaturePermission) environment.getSource()).getFeature().name());
b.addField().type(GraphQLBoolean).name("user").dataFetcher(environment -> ((FeaturePermission) environment.getSource()).isUser());
b.addField().type(GraphQLBoolean).name("info").dataFetcher(environment -> ((FeaturePermission) environment.getSource()).isInfo());
b.addField().type(GraphQLBoolean).name("admin").dataFetcher(environment -> ((FeaturePermission) environment.getSource()).isAdmin());
}
{ {
EntityGQLBuilder<EventUser> b = builder.createEntity(EventUser.class); EntityGQLBuilder<EventUser> b = builder.createEntity(EventUser.class);
b.addField(EventUser_.id); b.addField(EventUser_.id);
...@@ -327,8 +335,9 @@ public class MoyaGraphQLServlet extends HttpServlet { ...@@ -327,8 +335,9 @@ public class MoyaGraphQLServlet extends HttpServlet {
b.addField(EventUser_.event).type(builder.typeFor(SIMPLE_EVENT_TYPE_NAME)); b.addField(EventUser_.event).type(builder.typeFor(SIMPLE_EVENT_TYPE_NAME));
b.addField(EventUser_.eventuserCreated); b.addField(EventUser_.eventuserCreated);
b.addListField(Role.class).dataFetcher(environment -> userbean.findUsersRoles(environment.getSource())); b.addListField(Role.class).dataFetcher(environment -> userbean.findUsersRoles(environment.getSource()));
b.addListField(UsersEventUserproperty.class).dataFetcher(environment -> eventUserPropertyBean.getUserPropertiesForUser(environment.getSource()).stream().sorted(ENTITY_ID_SORTER).collect(toList())); b.addListField(UsersEventUserproperty.class).name("properties").dataFetcher(environment -> eventUserPropertyBean.getUserPropertiesForUser(environment.getSource()).stream().sorted(ENTITY_ID_SORTER).collect(toList()));
b.addField(EventUser_.currentPlaces); b.addField(EventUser_.currentPlaces);
b.addListField(FeaturePermission.class).name("permissions").dataFetcher(environment -> userbean.getUsersFeaturepermissions(environment.getSource()).values());
} }
{ {
...@@ -372,25 +381,25 @@ public class MoyaGraphQLServlet extends HttpServlet { ...@@ -372,25 +381,25 @@ public class MoyaGraphQLServlet extends HttpServlet {
b.addField().name("shirtSize").type(GraphQLString).dataFetcher(new JSONPropertyDataFetcher("meta", "shirtSize")); b.addField().name("shirtSize").type(GraphQLString).dataFetcher(new JSONPropertyDataFetcher("meta", "shirtSize"));
b.addField().name("allergiesFreetext").type(GraphQLString).dataFetcher(new JSONPropertyDataFetcher("meta", "allergies")); b.addField().name("allergiesFreetext").type(GraphQLString).dataFetcher(new JSONPropertyDataFetcher("meta", "allergies"));
b.addListField(UsersAllergy.class) b.addListField(UsersAllergy.class)
.name("allergies") .name("allergies")
.argument(newArgument() .argument(newArgument()
.description("By default, show only allergies, user has selected") .description("By default, show only allergies, user has selected")
.type(GraphQLBoolean) .type(GraphQLBoolean)
.defaultValue(true) .defaultValue(true)
.name("onlySelected")) .name("onlySelected"))
.dataFetcher(environment -> { .dataFetcher(environment -> {
List<UsersAllergy> allergies = allergybean.getUserAllergies((User) environment.getSource()); List<UsersAllergy> allergies = allergybean.getUserAllergies((User) environment.getSource());
logger.warn("Got allergies for user {}", allergies); logger.warn("Got allergies for user {}", allergies);
if (environment.getArgument("onlySelected")) { if (environment.getArgument("onlySelected")) {
logger.warn("Filtering allergies"); logger.warn("Filtering allergies");
return allergies.stream().sorted(ENTITY_ID_SORTER).filter(a -> a.isSelected()).collect(toList()); return allergies.stream().sorted(ENTITY_ID_SORTER).filter(a -> a.isSelected()).collect(toList());
} }
return allergies; return allergies;
}); });
b.addListField(EventUser.class) b.addListField(EventUser.class)
.dataFetcher(environment -> userbean.findAllEventusers(environment.getSource()).stream().sorted(ENTITY_ID_SORTER).collect(toList())) .dataFetcher(environment -> userbean.findAllEventusers(environment.getSource()).stream().sorted(ENTITY_ID_SORTER).collect(toList()))
.description("Only users themselves can fetch eventusers for other events. If another user tries to fetch this data, an exception will be thrown"); .description("Only users themselves can fetch eventusers for other events. If another user tries to fetch this data, an exception will be thrown");
} }
...@@ -405,9 +414,9 @@ public class MoyaGraphQLServlet extends HttpServlet { ...@@ -405,9 +414,9 @@ public class MoyaGraphQLServlet extends HttpServlet {
b.addField(UsersAllergy_.selected); b.addField(UsersAllergy_.selected);
b.addField().type(GraphQLString) b.addField().type(GraphQLString)
.name("allergyname") .name("allergyname")
.dataFetcher(environment -> ((UsersAllergy) environment.getSource()).getAllergy().getName().getDefaultValue()) .dataFetcher(environment -> ((UsersAllergy) environment.getSource()).getAllergy().getName().getDefaultValue())
.description("Shorthand for { allergy {name {defaultvalue}}}"); .description("Shorthand for { allergy {name {defaultvalue}}}");
} }
{ {
...@@ -632,102 +641,107 @@ public class MoyaGraphQLServlet extends HttpServlet { ...@@ -632,102 +641,107 @@ public class MoyaGraphQLServlet extends HttpServlet {
schemaBld.additionalTypes(builder.getTypes()); schemaBld.additionalTypes(builder.getTypes());
schemaBld.query(GraphQLObjectType.newObject() schemaBld.query(GraphQLObjectType.newObject()
.name("moyaQuery") .name("moyaQuery")
.field(getUserSearchField(builder)) .field(getUserSearchField(builder))
.field(getEventSearchQuery(builder)) .field(getEventSearchQuery(builder))
.field(getSingleUserQuery(builder)) .field(getSingleUserQuery(builder))
.field( .field(
newFieldDefinition() newFieldDefinition()
.name("eventmaps") .name("eventmaps")
.type(GraphQLList.list(builder.typeFor(EventMap.class))) .type(GraphQLList.list(builder.typeFor(EventMap.class)))
.dataFetcher(environment -> placebean.getMaps().stream().sorted(ENTITY_ID_SORTER).collect(toList()))) .dataFetcher(environment -> placebean.getMaps().stream().sorted(ENTITY_ID_SORTER).collect(toList())))
.field( .field(
newFieldDefinition() newFieldDefinition()
.name("roles") .name("roles")
.type(GraphQLList.list(builder.typeFor(Role.class))) .type(GraphQLList.list(builder.typeFor(Role.class)))
.dataFetcher(environment -> rolebean.listRoles().stream().sorted(ENTITY_ID_SORTER).collect(toList()))) .dataFetcher(environment -> rolebean.listRoles().stream().sorted(ENTITY_ID_SORTER).collect(toList())))
.field( .field(
newFieldDefinition() newFieldDefinition()
.name("products") .name("products")
.type(GraphQLList.list(builder.typeFor(Product.class))) .type(GraphQLList.list(builder.typeFor(Product.class)))
.dataFetcher(environment -> productbean.findProductsForEvent().stream().sorted(ENTITY_ID_SORTER).collect(toList()))) .dataFetcher(environment -> productbean.findProductsForEvent().stream().sorted(ENTITY_ID_SORTER).collect(toList())))
.field( .field(
newFieldDefinition() newFieldDefinition()
.name("compos") .name("compos")
.type(GraphQLList.list(builder.typeFor(Compo.class))) .type(GraphQLList.list(builder.typeFor(Compo.class)))
.dataFetcher(environment -> votebean.getCompoList(true).stream().sorted(ENTITY_ID_SORTER).collect(toList()))) .dataFetcher(environment -> votebean.getCompoList(true).stream().sorted(ENTITY_ID_SORTER).collect(toList())))
.field( .field(
newFieldDefinition() newFieldDefinition()
.name("vips") .name("vips")
.type(GraphQLList.list(builder.typeFor(Vip.class))) .type(GraphQLList.list(builder.typeFor(Vip.class)))
.dataFetcher(environment -> vipBean.getAvailableVips())) .dataFetcher(environment -> vipBean.getAvailableVips()))
.field(
newFieldDefinition()
.name("features")
.description("Fetch all features available in moya")
.type(GraphQLList.list(GraphQLString))
.dataFetcher(environment -> Arrays.asList(MoyaFeature.values())))
); );
return schemaBld.build(); return schemaBld.build();
} }
private GraphQLFieldDefinition getSingleUserQuery(GraphQLBuilder builder) { private GraphQLFieldDefinition getSingleUserQuery(GraphQLBuilder builder) {
return newFieldDefinition() return newFieldDefinition()
.name("user") .name("user")
.argument(newArgument().name("id").defaultValue(null).type(GraphQLInt).description("Id of the global user object. If id is 0, currently logged in user is used")) .argument(newArgument().name("id").defaultValue(null).type(GraphQLInt).description("Id of the global user object. If id is 0, currently logged in user is used"))
.type(builder.typeFor(EventUser.class)) .type(builder.typeFor(EventUser.class))
.dataFetcher(environment -> { .dataFetcher(environment -> {
Integer id = environment.getArgument("userId"); Integer id = environment.getArgument("userId");
EventUser user; EventUser user;
if (id == null || id.equals(0)) { if (id == null || id.equals(0)) {
user = permbean.getCurrentUser(); user = permbean.getCurrentUser();
} else { } else {
user = userbean.findByUserId(id, false); user = userbean.findByUserId(id, false);
} }
if (user == null) { if (user == null) {
throw new NullPointerException("User not found with id " + id); throw new NullPointerException("User not found with id " + id);
} }
return user; return user;
}).build(); }).build();
} }
private GraphQLFieldDefinition.Builder getEventSearchQuery(GraphQLBuilder builder) { private GraphQLFieldDefinition.Builder getEventSearchQuery(GraphQLBuilder builder) {
return newFieldDefinition() return newFieldDefinition()
.name("currentevent") .name("currentevent")
.type(builder.typeFor(LanEvent.class)) .type(builder.typeFor(LanEvent.class))
.dataFetcher(environment -> eventbean.getCurrentEvent()); .dataFetcher(environment -> eventbean.getCurrentEvent());
} }
private GraphQLFieldDefinition.Builder getUserSearchField(GraphQLBuilder builder) { private GraphQLFieldDefinition.Builder getUserSearchField(GraphQLBuilder builder) {
return newFieldDefinition() return newFieldDefinition()
.type(new GraphQLList(builder.typeFor(EventUser.class))) .type(new GraphQLList(builder.typeFor(EventUser.class)))
.name("usersearch") .name("usersearch")
.description("Search users. Using this method requires admin acccess to the database") .description("Search users. Using this method requires admin acccess to the database")
//.argument(newArgument().name("reason").type(GraphQLString).description("Reason for the data request. Reason and requested fields are stored to audit log.")) //.argument(newArgument().name("reason").type(GraphQLString).description("Reason for the data request. Reason and requested fields are stored to audit log."))
.argument(newArgument().name("roles").defaultValue(Collections.emptyList()).description("(Optional) Filter users that belong to a role").type(GraphQLList.list(GraphQLInt))) .argument(newArgument().name("roles").defaultValue(Collections.emptyList()).description("(Optional) Filter users that belong to a role").type(GraphQLList.list(GraphQLInt)))
.argument(newArgument().name("search").type(GraphQLString).defaultValue("").description("(Optional) Filter users that contain the search parameter in their firstname, lastname, nick or email").type(GraphQLString)) .argument(newArgument().name("search").type(GraphQLString).defaultValue("").description("(Optional) Filter users that contain the search parameter in their firstname, lastname, nick or email").type(GraphQLString))
.argument(newArgument().name("page").type(GraphQLInt).defaultValue(0).description("Pagination page ( 0 is the first page)")) .argument(newArgument().name("page").type(GraphQLInt).defaultValue(0).description("Pagination page ( 0 is the first page)"))
.argument(newArgument().name("pagesize").type(GraphQLInt).defaultValue(0).description("Pagination pagesize (0 disables pagination)")) .argument(newArgument().name("pagesize").type(GraphQLInt).defaultValue(0).description("Pagination pagesize (0 disables pagination)"))
.dataFetcher((DataFetcher<List<EventUser>>) environment -> { .dataFetcher((DataFetcher<List<EventUser>>) environment -> {
/* /*
String reason = environment.getArgument("reason"); String reason = environment.getArgument("reason");
if (reason == null || reason.isEmpty()) { if (reason == null || reason.isEmpty()) {
throw new GRPDException("Query did not provide a reason for the request"); throw new GRPDException("Query did not provide a reason for the request");
}*/ }*/
UserSearchQuery query = new UserSearchQuery(environment.getArgument("page"), environment.getArgument("pagesize"), null, null, SearchQuery.QuerySortOrder.UNSORTED); UserSearchQuery query = new UserSearchQuery(environment.getArgument("page"), environment.getArgument("pagesize"), null, null, SearchQuery.QuerySortOrder.UNSORTED);
String searchArg = environment.getArgument("search"); String searchArg = environment.getArgument("search");
if (searchArg != null && !searchArg.isEmpty()) { if (searchArg != null && !searchArg.isEmpty()) {
query.setSearch(searchArg); query.setSearch(searchArg);
} }
List<Integer> roleArgument = environment.getArgument("roles"); List<Integer> roleArgument = environment.getArgument("roles");
if (roleArgument != null && !roleArgument.isEmpty()) { if (roleArgument != null && !roleArgument.isEmpty()) {
query.setFilterRoles(roleArgument.stream().map(id -> rolebean.find(id)).filter(x -> x != null).collect(toList())); query.setFilterRoles(roleArgument.stream().map(id -> rolebean.find(id)).filter(x -> x != null).collect(toList()));
} }
SearchResult<EventUser> ret = userbean.getThisEventsUsers(query); SearchResult<EventUser> ret = userbean.getThisEventsUsers(query);
return ret.getResults(); return ret.getResults();
}); });
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!