/// <summary> /// Returns a planet member given the user and planet id /// </summary> public async Task <TaskResult <PlanetMember> > GetPlanetMember(ulong user_id, ulong planet_id, string auth) { // Retrieve planet ServerPlanet planet = ServerPlanet.FromBase(await Context.Planets.FindAsync(planet_id)); if (planet == null) { return(new TaskResult <PlanetMember>(false, "The planet could not be found.", null)); } // Authentication flow AuthToken token = await Context.AuthTokens.FindAsync(auth); // If authorizor is not a member of the planet, they do not have authority to get member info if (token == null || !(await planet.IsMemberAsync(token.User_Id))) { return(new TaskResult <PlanetMember>(false, "Failed to authorize.", null)); } // At this point the request is authorized PlanetMember member = await Context.PlanetMembers.FirstOrDefaultAsync(x => x.User_Id == user_id && x.Planet_Id == planet_id); if (member == null) { return(new TaskResult <PlanetMember>(false, "Could not find member.", null)); } return(new TaskResult <PlanetMember>(true, "Successfully retrieved planet user.", member)); }
/// <summary> /// Creates a PlanetUser instance using a user id and planet id /// </summary> public static async Task <ServerPlanetUser> CreateAsync(ulong userid, ulong planet_id, IMapper mapper) { using (ValourDB db = new ValourDB(ValourDB.DBOptions)) { // Retrieve user User user = await db.Users.FindAsync(userid); // Retrieve planet ServerPlanet planet = ServerPlanet.FromBase(await db.Planets.FindAsync(planet_id), mapper); // TODO: Actually set roles and stuff once roles exist. // Ensure user is within planet if (!(await planet.IsMemberAsync(user))) { return(null); } // First map the user to a planetUser to copy basic fields ServerPlanetUser planetUser = mapper.Map <ServerPlanetUser>(user); // Now copy across planet info planetUser.Planet_Id = planet_id; return(planetUser); } }
/// <summary> /// Returns a planetuser given the user and planet id /// </summary> public async Task <TaskResult <PlanetUser> > GetPlanetUser(ulong userid, ulong planet_id, string auth) { // Retrieve planet ServerPlanet planet = ServerPlanet.FromBase(await Context.Planets.FindAsync(planet_id), Mapper); if (planet == null) { return(new TaskResult <PlanetUser>(false, "The planet could not be found.", null)); } // Authentication flow AuthToken token = await Context.AuthTokens.FindAsync(auth); // If authorizor is not a member of the planet, they do not have authority to get member info if (token == null || !(await planet.IsMemberAsync(token.User_Id))) { return(new TaskResult <PlanetUser>(false, "Failed to authorize.", null)); } // At this point the request is authorized // Retrieve server data for user User user = await Context.Users.FindAsync(userid); // Null check if (user == null) { return(new TaskResult <PlanetUser>(false, "The user could not be found.", null)); } // Ensure the user is a member of the planet if (!(await planet.IsMemberAsync(user))) { return(new TaskResult <PlanetUser>(false, "The target user is not a member of the planet.", null)); } PlanetUser planetUser = await ServerPlanetUser.CreateAsync(userid, planet_id, Mapper); if (planetUser == null) { return(new TaskResult <PlanetUser>(false, "Could not create planet user: Fatal error.", null)); } return(new TaskResult <PlanetUser>(true, "Successfully retrieved planet user.", planetUser)); }
/// <summary> /// Returns a planet member's role ids given the user and planet id /// </summary> public async Task <TaskResult <List <ulong> > > GetPlanetMemberRoleIds(ulong user_id, ulong planet_id, string token) { // Retrieve planet ServerPlanet planet = ServerPlanet.FromBase(await Context.Planets.FindAsync(planet_id)); if (planet == null) { return(new TaskResult <List <ulong> >(false, "The planet could not be found.", null)); } // Authentication flow AuthToken authToken = await Context.AuthTokens.FindAsync(token); // If authorizor is not a member of the planet, they do not have authority to get member info if (authToken == null || !(await planet.IsMemberAsync(authToken.User_Id))) { return(new TaskResult <List <ulong> >(false, "Failed to authorize.", null)); } var roles = Context.PlanetRoleMembers.Include(x => x.Role).Where(x => x.User_Id == user_id && x.Planet_Id == planet_id).OrderBy(x => x.Role.Position); var roleids = roles.Select(x => x.Role_Id).ToList(); return(new TaskResult <List <ulong> >(true, $"Retrieved {roleids.Count} roles.", roleids)); }