public async Task <TaskResult> SetDescription(string description, ulong id, ulong userid, string token) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); // Return the same if the token is for the wrong user to prevent someone // from knowing if they cracked another user's token. This is basically // impossible to happen by chance but better safe than sorry in the case that // the literal impossible odds occur, more likely someone gets a stolen token // but is not aware of the owner but I'll shut up now - Spike if (authToken == null || authToken.User_Id != userid) { return(new TaskResult(false, "Failed to authorize user.")); } PlanetChatChannel channel = await Context.PlanetChatChannels.Where(x => x.Id == id).FirstOrDefaultAsync(); ServerPlanet planet = await ServerPlanet.FindAsync(channel.Planet_Id, Mapper); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageChannels))) { return(new TaskResult(false, "You are not authorized to do this.")); } channel.Description = description; await Context.SaveChangesAsync(); return(new TaskResult(true, "Successfully set description.")); }
/// <summary> /// Returns a role in a planet /// </summary> public async Task <TaskResult <PlanetRole> > GetPlanetRole(ulong role_id, string token) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); PlanetRole role = await Context.PlanetRoles.FindAsync(role_id); if (authToken == null) { return(new TaskResult <PlanetRole>(false, "Failed to authorize user.", null)); } ServerPlanet planet = await ServerPlanet.FindAsync(role.Planet_Id); if (planet == null) { return(new TaskResult <PlanetRole>(false, $"Could not find planet {role.Planet_Id}", null)); } if (!(await planet.IsMemberAsync(authToken.User_Id))) { return(new TaskResult <PlanetRole>(false, "Failed to authorize user.", null)); } return(new TaskResult <PlanetRole>(true, $"Retrieved role.", role)); }
public async Task <TaskResult <List <PlanetInvite> > > GetInvites(ulong userid, string token, ulong planet_id) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); // Return the same if the token is for the wrong user to prevent someone // from knowing if they cracked another user's token. This is basically // impossible to happen by chance but better safe than sorry in the case that // the literal impossible odds occur, more likely someone gets a stolen token // but is not aware of the owner but I'll shut up now - Spike if (authToken == null || authToken.User_Id != userid) { return(new TaskResult <List <PlanetInvite> >(false, "Failed to authorize user.", null)); } ServerPlanet planet = await ServerPlanet.FindAsync(planet_id, Mapper); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.Invite))) { return(new TaskResult <List <PlanetInvite> >(false, "You are not authorized to do this.", null)); } List <PlanetInvite> invites = await Task.Run(() => Context.PlanetInvites.Where(x => x.Planet_Id == planet_id).ToList()); return(new TaskResult <List <PlanetInvite> >(true, $"Retrieved {invites.Count} invites", invites)); }
public async Task <TaskResult> SetName(string name, ulong id, ulong user_id, string token) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); // Return the same if the token is for the wrong user to prevent someone // from knowing if they cracked another user's token. This is basically // impossible to happen by chance but better safe than sorry in the case that // the literal impossible odds occur, more likely someone gets a stolen token // but is not aware of the owner but I'll shut up now - Spike if (authToken == null || authToken.User_Id != user_id) { return(new TaskResult(false, "Failed to authorize user.")); } PlanetCategory category = await Context.PlanetCategories.Where(x => x.Id == id).FirstOrDefaultAsync(); ServerPlanet planet = await ServerPlanet.FindAsync(category.Planet_Id); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageCategories))) { return(new TaskResult(false, "You are not authorized to do this.")); } category.Name = name; await Context.SaveChangesAsync(); await PlanetHub.Current.Clients.Group($"p-{category.Planet_Id}").SendAsync("RefreshChannelList", ""); return(new TaskResult(true, "Successfully set name.")); }
public async Task <TaskResult <PlanetInvite> > CreateInvite(ulong Planet_Id, ulong userid, string token, int hours) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); // Return the same if the token is for the wrong user to prevent someone // from knowing if they cracked another user's token. This is basically // impossible to happen by chance but better safe than sorry in the case that // the literal impossible odds occur, more likely someone gets a stolen token // but is not aware of the owner but I'll shut up now - Spike if (authToken == null || authToken.User_Id != userid) { return(new TaskResult <PlanetInvite>(false, "Failed to authorize user.", null)); } ServerPlanet planet = await ServerPlanet.FindAsync(Planet_Id, Mapper); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.Invite))) { return(new TaskResult <PlanetInvite>(false, "You are not authorized to do this.", null)); } PlanetInvite invite = new PlanetInvite() { Planet_Id = Planet_Id, Issuer_Id = userid, Time = DateTime.UtcNow, Hours = hours }; Random random = new Random(); const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string code = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray()); PlanetInvite test = await Context.PlanetInvites.FirstOrDefaultAsync(x => x.Code == code); while (test != null) { code = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray()); test = await Context.PlanetInvites.Where(x => x.Code == code).FirstOrDefaultAsync(); } invite.Code = code; if (hours == 0) { invite.Hours = null; } await Context.PlanetInvites.AddAsync(invite); await Context.SaveChangesAsync(); return(new TaskResult <PlanetInvite>(true, $"Successfully created invite", invite)); }
/// <summary> /// Creates a server and if successful returns a task result with the created /// planet's id /// </summary> public async Task <TaskResult <ulong> > CreateChannel(string name, ulong planet_id, ulong user_id, ulong parentid, string token) { TaskResult nameValid = ValidateName(name); if (!nameValid.Success) { return(new TaskResult <ulong>(false, nameValid.Message, 0)); } AuthToken authToken = await Context.AuthTokens.FindAsync(token); // Return the same if the token is for the wrong user to prevent someone // from knowing if they cracked another user's token. This is basically // impossible to happen by chance but better safe than sorry in the case that // the literal impossible odds occur, more likely someone gets a stolen token // but is not aware of the owner but I'll shut up now - Spike if (authToken == null || authToken.User_Id != user_id) { return(new TaskResult <ulong>(false, "Failed to authorize user.", 0)); } ServerPlanet planet = await ServerPlanet.FindAsync(planet_id); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageChannels))) { return(new TaskResult <ulong>(false, "You are not authorized to do this.", 0)); } // User is verified and given channel info is valid by this point // Creates the channel channel PlanetChatChannel channel = new PlanetChatChannel() { Id = IdManager.Generate(), Name = name, Planet_Id = planet_id, Parent_Id = parentid, Message_Count = 0, Description = "A chat channel", Position = Convert.ToUInt16(Context.PlanetChatChannels.Count()) }; // Add channel to database await Context.PlanetChatChannels.AddAsync(channel); // Save changes to DB await Context.SaveChangesAsync(); await PlanetHub.Current.Clients.Group($"p-{planet_id}").SendAsync("RefreshChannelList", ""); // Return success return(new TaskResult <ulong>(true, "Successfully created channel.", channel.Id)); }
public async Task <TaskResult> BanUser(ulong target_id, ulong planet_id, string reason, string token, uint time) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); if (authToken == null) { return(new TaskResult(false, "Failed to authorize user.")); } ServerPlanet planet = await ServerPlanet.FindAsync(planet_id); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.Ban))) { return(new TaskResult(false, "You are not authorized to do this.")); } ServerPlanetMember member = await Context.PlanetMembers.FirstOrDefaultAsync(x => x.Id == target_id && x.Planet_Id == planet_id); PlanetBan ban = new PlanetBan() { Id = IdManager.Generate(), Reason = reason, Planet_Id = planet_id, User_Id = member.User_Id, Banner_Id = authToken.User_Id, Time = DateTime.UtcNow, Permanent = false }; if (time <= 0) { ban.Permanent = true; } else { ban.Minutes = time; } // Add channel to database await Context.PlanetBans.AddAsync(ban); List <ServerPlanetRoleMember> roles = await Task.Run(() => Context.PlanetRoleMembers.Where(x => x.Member_Id == target_id && x.Planet_Id == planet_id).ToList()); foreach (ServerPlanetRoleMember role in roles) { Context.PlanetRoleMembers.Remove(role); } Context.PlanetMembers.Remove(member); await Context.SaveChangesAsync(); return(new TaskResult(true, $"Successfully banned user {member.Nickname}")); }
/// <summary> /// Returns a planet's primary channel /// </summary> public async Task <TaskResult <PlanetChatChannel> > GetPrimaryChannel(ulong planetid, ulong userid, string token) { ServerPlanet planet = await ServerPlanet.FindAsync(planetid, Mapper); if (!(await planet.AuthorizedAsync(userid, token))) { return(new TaskResult <PlanetChatChannel>(false, "You are not authorized to access this planet.", null)); } return(new TaskResult <PlanetChatChannel>(true, "Successfully retireved channel.", await planet.GetPrimaryChannelAsync())); }
public async Task <TaskResult> UpdateOrder([FromBody] Dictionary <ushort, List <ulong> > json, ulong user_id, string token, ulong Planet_Id) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); // Return the same if the token is for the wrong user to prevent someone // from knowing if they cracked another user's token. This is basically // impossible to happen by chance but better safe than sorry in the case that // the literal impossible odds occur, more likely someone gets a stolen token // but is not aware of the owner but I'll shut up now - Spike if (authToken == null || authToken.User_Id != user_id) { return(new TaskResult(false, "Failed to authorize user.")); } ServerPlanet planet = await ServerPlanet.FindAsync(Planet_Id); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageChannels))) { return(new TaskResult(false, "You are not authorized to do this.")); } Console.WriteLine(json); var values = json;//JsonConvert.DeserializeObject<Dictionary<ulong, ulong>>(data); foreach (var value in values) { ushort position = value.Key; ulong id = value.Value[0]; //checks if item is a channel Console.WriteLine(value.Value[0]); if (value.Value[1] == 0) { PlanetChatChannel channel = await Context.PlanetChatChannels.Where(x => x.Id == id).FirstOrDefaultAsync(); channel.Position = position; } if (value.Value[1] == 1) { PlanetCategory category = await Context.PlanetCategories.Where(x => x.Id == id).FirstOrDefaultAsync(); category.Position = position; } Console.WriteLine(value); } await Context.SaveChangesAsync(); await PlanetHub.Current.Clients.Group($"p-{Planet_Id}").SendAsync("RefreshChannelList", ""); return(new TaskResult(true, "Updated Order!")); }
public async Task <TaskResult <PlanetInvite> > CreateInvite(ulong Planet_Id, string token, int hours) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); if (authToken == null) { return(new TaskResult <PlanetInvite>(false, "Failed to authorize user.", null)); } ServerPlanet planet = await ServerPlanet.FindAsync(Planet_Id); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.Invite))) { return(new TaskResult <PlanetInvite>(false, "You are not authorized to do this.", null)); } PlanetInvite invite = new PlanetInvite() { Id = IdManager.Generate(), Planet_Id = Planet_Id, Issuer_Id = authToken.User_Id, Time = DateTime.UtcNow, Hours = hours }; Random random = new Random(); const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string code = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray()); PlanetInvite test = await Context.PlanetInvites.FirstOrDefaultAsync(x => x.Code == code); while (test != null) { code = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray()); test = await Context.PlanetInvites.Where(x => x.Code == code).FirstOrDefaultAsync(); } invite.Code = code; if (hours == 0) { invite.Hours = null; } await Context.PlanetInvites.AddAsync(invite); await Context.SaveChangesAsync(); return(new TaskResult <PlanetInvite>(true, $"Successfully created invite", invite)); }
public async Task <TaskResult> BanUser(ulong id, ulong Planet_Id, string reason, ulong userid, string token, uint time) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); // Return the same if the token is for the wrong user to prevent someone // from knowing if they cracked another user's token. This is basically // impossible to happen by chance but better safe than sorry in the case that // the literal impossible odds occur, more likely someone gets a stolen token // but is not aware of the owner but I'll shut up now - Spike if (authToken == null || authToken.User_Id != userid) { return(new TaskResult(false, "Failed to authorize user.")); } ServerPlanet planet = await ServerPlanet.FindAsync(Planet_Id, Mapper); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.Ban))) { return(new TaskResult(false, "You are not authorized to do this.")); } PlanetBan ban = new PlanetBan() { Reason = reason, Planet_Id = Planet_Id, User_Id = id, Banner_Id = userid, Time = DateTime.UtcNow, Permanent = false }; if (time <= 0) { ban.Permanent = true; } else { ban.Minutes = time; } // Add channel to database await Context.PlanetBans.AddAsync(ban); PlanetMember member = await Context.PlanetMembers.Where(x => x.User_Id == id).FirstOrDefaultAsync(); Context.PlanetMembers.Remove(member); await Context.SaveChangesAsync(); return(new TaskResult(true, $"Successfully banned user {id}")); }
/// <summary> /// Creates a server and if successful returns a task result with the created /// planet's id /// </summary> public async Task <TaskResult <ulong> > CreateCategory(string name, ulong userid, ulong parentid, ulong planet_id, string token) { TaskResult nameValid = ValidateName(name); if (!nameValid.Success) { return(new TaskResult <ulong>(false, nameValid.Message, 0)); } AuthToken authToken = await Context.AuthTokens.FindAsync(token); // Return the same if the token is for the wrong user to prevent someone // from knowing if they cracked another user's token. This is basically // impossible to happen by chance but better safe than sorry in the case that // the literal impossible odds occur, more likely someone gets a stolen token // but is not aware of the owner but I'll shut up now - Spike if (authToken == null || authToken.User_Id != userid) { return(new TaskResult <ulong>(false, "Failed to authorize user.", 0)); } ServerPlanet planet = await ServerPlanet.FindAsync(planet_id, Mapper); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageCategories))) { return(new TaskResult <ulong>(false, "You are not authorized to do this.", 0)); } // User is verified and given channel info is valid by this point // Creates the channel channel PlanetCategory category = new PlanetCategory() { Name = name, Planet_Id = planet_id, Parent_Id = parentid }; // Add channel to database await Context.PlanetCategories.AddAsync(category); // Save changes to DB await Context.SaveChangesAsync(); // Return success return(new TaskResult <ulong>(true, "Successfully created category.", category.Id)); }
/// <summary> /// Sets the description of a planet /// </summary> public async Task <TaskResult> SetPublic(ulong planet_id, bool ispublic, string token) { ServerPlanet planet = await ServerPlanet.FindAsync(planet_id); if (!(await planet.AuthorizedAsync(token, PlanetPermissions.Manage))) { return(new TaskResult(false, "You are not authorized to manage this planet.")); } planet.Public = ispublic; Context.Planets.Update(planet); await Context.SaveChangesAsync(); return(new TaskResult(true, "Changed public value successfully")); }
/// <summary> /// Sets the description of a planet /// </summary> public async Task <TaskResult> SetDescription(ulong planet_id, string description, string token) { ServerPlanet planet = await ServerPlanet.FindAsync(planet_id, Mapper); if (!(await planet.AuthorizedAsync(token, PlanetPermissions.Manage))) { return(new TaskResult(false, "You are not authorized to manage this planet.")); } planet.Description = description; Context.Planets.Update(planet); await Context.SaveChangesAsync(); return(new TaskResult(true, "Changed description successfully")); }
/// <summary> /// Returns a planet object (if permitted) /// </summary> public async Task <TaskResult <Planet> > GetPlanet(ulong planetid, ulong userid, string token) { ServerPlanet planet = await ServerPlanet.FindAsync(planetid, Mapper); if (planet == null) { return(new TaskResult <Planet>(false, "The given planet id does not exist.", null)); } if (!(await planet.AuthorizedAsync(userid, token))) { return(new TaskResult <Planet>(false, "You are not authorized to access this planet.", null)); } return(new TaskResult <Planet>(true, "Successfully retrieved planet.", planet)); }
public async Task <TaskResult> Delete(ulong id, ulong user_id, string token) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); // Return the same if the token is for the wrong user to prevent someone // from knowing if they cracked another user's token. This is basically // impossible to happen by chance but better safe than sorry in the case that // the literal impossible odds occur, more likely someone gets a stolen token // but is not aware of the owner but I'll shut up now - Spike if (authToken == null || authToken.User_Id != user_id) { return(new TaskResult(false, "Failed to authorize user.")); } PlanetChatChannel channel = await Context.PlanetChatChannels.FindAsync(id); if (channel == null) { return(new TaskResult(false, "That channel does not exist.")); } ServerPlanet planet = await ServerPlanet.FindAsync(channel.Planet_Id); if (planet == null) { return(new TaskResult(false, "Could not find the planet.")); } if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageChannels))) { return(new TaskResult(false, "You are not authorized to do this.")); } if (channel.Id == planet.Main_Channel_Id) { return(new TaskResult(false, "You can not delete your main channel!")); } Context.PlanetChatChannels.Remove(channel); await Context.SaveChangesAsync(); await PlanetHub.Current.Clients.Group($"p-{channel.Planet_Id}").SendAsync("RefreshChannelList", ""); return(new TaskResult(true, "Successfully deleted.")); }
/// <summary> /// Returns the planet membership of a user /// </summary> /// <param name="token"></param> /// <returns></returns> public async Task <TaskResult <List <Planet> > > GetPlanetMembership(ulong user_id, string token) { if (token == null) { return(new TaskResult <List <Planet> >(false, "Please supply an authentication token", null)); } AuthToken authToken = await Context.AuthTokens.FindAsync(token); if (!Permission.HasPermission(authToken.Scope, UserPermissions.Membership)) { return(new TaskResult <List <Planet> >(false, $"The given token does not have membership scope", null)); } User user = await Context.Users.FindAsync(user_id); if (user == null) { return(new TaskResult <List <Planet> >(false, $"Could not find user {user_id}", null)); } List <Planet> membership = new List <Planet>(); ServerPlanet valourServer = await ServerPlanet.FindAsync(735703679107072); if (valourServer != null) { // Remove this after pre-pre-alpha if (!(await valourServer.IsMemberAsync(user_id, Context))) { await valourServer.AddMemberAsync(user, Context); } } foreach (PlanetMember member in Context.PlanetMembers.Where(x => x.User_Id == user_id)) { Planet planet = await Context.Planets.FindAsync(member.Planet_Id); if (planet != null) { membership.Add(planet); } } return(new TaskResult <List <Planet> >(true, $"Retrieved {membership.Count} planets", membership)); }
/// <summary> /// Returns a planet object (if permitted) /// </summary> public async Task <TaskResult <Planet> > GetPlanet(ulong planet_id, string token) { ServerPlanet planet = await ServerPlanet.FindAsync(planet_id); if (planet == null) { return(new TaskResult <Planet>(false, "The given planet id does not exist.", null)); } AuthToken authToken = await Context.AuthTokens.FindAsync(token); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.View))) { return(new TaskResult <Planet>(false, "You are not authorized to access this planet.", null)); } return(new TaskResult <Planet>(true, "Successfully retrieved planet.", planet)); }
public async Task <TaskResult> Delete(ulong id, ulong userid, string token) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); // Return the same if the token is for the wrong user to prevent someone // from knowing if they cracked another user's token. This is basically // impossible to happen by chance but better safe than sorry in the case that // the literal impossible odds occur, more likely someone gets a stolen token // but is not aware of the owner but I'll shut up now - Spike if (authToken == null || authToken.User_Id != userid) { return(new TaskResult(false, "Failed to authorize user.")); } PlanetCategory category = await Context.PlanetCategories.FindAsync(id); ServerPlanet planet = await ServerPlanet.FindAsync(category.Planet_Id, Mapper); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageCategories))) { return(new TaskResult(false, "You are not authorized to do this.")); } List <PlanetCategory> categories = await Task.Run(() => Context.PlanetCategories.Where(x => x.Parent_Id == id).ToList()); foreach (PlanetCategory Category in categories) { Category.Parent_Id = null; } List <PlanetChatChannel> channels = await Task.Run(() => Context.PlanetChatChannels.Where(x => x.Parent_Id == id).ToList()); foreach (PlanetChatChannel channel in channels) { Context.PlanetChatChannels.Remove(channel); } Context.PlanetCategories.Remove(category); Context.PlanetCategories.Remove(category); await Context.SaveChangesAsync(); return(new TaskResult(true, "Successfully deleted.")); }
public async Task <TaskResult <List <PlanetInvite> > > GetInvites(ulong user_id, string token, ulong planet_id) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); if (authToken == null) { return(new TaskResult <List <PlanetInvite> >(false, "Failed to authorize user.", null)); } ServerPlanet planet = await ServerPlanet.FindAsync(planet_id); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.Invite))) { return(new TaskResult <List <PlanetInvite> >(false, "You are not authorized to do this.", null)); } List <PlanetInvite> invites = await Task.Run(() => Context.PlanetInvites.Where(x => x.Planet_Id == planet_id).ToList()); return(new TaskResult <List <PlanetInvite> >(true, $"Retrieved {invites.Count} invites", invites)); }
public async Task <TaskResult> Join(string code, string token) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); if (authToken == null) { return(new TaskResult(false, $"Unable to authorize")); } ulong user_id = authToken.User_Id; PlanetInvite invite = await Context.PlanetInvites.FirstOrDefaultAsync(x => x.Code == code); if (invite == null) { return(new TaskResult(false, $"Invite code not found!")); } if (await Context.PlanetBans.AnyAsync(x => x.User_Id == user_id && x.Planet_Id == invite.Planet_Id)) { return(new TaskResult(false, $"User is banned from this planet!")); } if (await Context.PlanetMembers.AnyAsync(x => x.User_Id == user_id && x.Planet_Id == invite.Planet_Id)) { return(new TaskResult(false, $"User is already in this planet!")); } ServerPlanet planet = await ServerPlanet.FindAsync(invite.Planet_Id); if (!planet.Public) { return(new TaskResult(false, $"Planet is set to private!")); } User user = await Context.Users.FindAsync(user_id); await planet.AddMemberAsync(user, Context); return(new TaskResult(true, $"Successfully joined planet.")); }
/// <summary> /// Sets the name of a planet /// </summary> public async Task <TaskResult> SetName(ulong planet_id, string name, string token) { ServerPlanet planet = await ServerPlanet.FindAsync(planet_id); if (!(await planet.AuthorizedAsync(token, PlanetPermissions.Manage))) { return(new TaskResult(false, "You are not authorized to manage this planet.")); } TaskResult validation = ValidateName(name); if (!validation.Success) { return(validation); } planet.Name = name; Context.Planets.Update(planet); await Context.SaveChangesAsync(); return(new TaskResult(true, "Changed name successfully")); }
public async Task <TaskResult> KickUser(ulong target_id, ulong planet_id, string token) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); if (authToken == null) { return(new TaskResult(false, "Failed to authorize user.")); } ServerPlanet planet = await ServerPlanet.FindAsync(planet_id); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.Kick))) { return(new TaskResult(false, "You are not authorized to do this.")); } ServerPlanetMember member = await Context.PlanetMembers.FirstOrDefaultAsync(x => x.Id == target_id && x.Planet_Id == planet_id); if (member == null) { return(new TaskResult(true, $"Could not find PlanetMember {target_id}")); } List <ServerPlanetRoleMember> roles = await Task.Run(() => Context.PlanetRoleMembers.Where(x => x.Member_Id == target_id && x.Planet_Id == planet_id).ToList()); foreach (ServerPlanetRoleMember role in roles) { Context.PlanetRoleMembers.Remove(role); } Context.PlanetMembers.Remove(member); await Context.SaveChangesAsync(); return(new TaskResult(true, $"Successfully kicked user {target_id}")); }
public async Task <TaskResult> Delete(ulong id, ulong user_id, string token) { AuthToken authToken = await Context.AuthTokens.FindAsync(token); // Return the same if the token is for the wrong user to prevent someone // from knowing if they cracked another user's token. This is basically // impossible to happen by chance but better safe than sorry in the case that // the literal impossible odds occur, more likely someone gets a stolen token // but is not aware of the owner but I'll shut up now - Spike if (authToken == null || authToken.User_Id != user_id) { return(new TaskResult(false, "Failed to authorize user.")); } PlanetCategory category = await Context.PlanetCategories.FindAsync(id); ServerPlanet planet = await ServerPlanet.FindAsync(category.Planet_Id); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageCategories))) { return(new TaskResult(false, "You are not authorized to do this.")); } List <PlanetCategory> cate = await Task.Run(() => Context.PlanetCategories.Where(x => x.Planet_Id == category.Planet_Id).ToList()); if (cate.Count == 1) { return(new TaskResult(false, "You can not delete your last category!")); } List <PlanetCategory> categories = await Task.Run(() => Context.PlanetCategories.Where(x => x.Parent_Id == id).ToList()); List <PlanetChatChannel> channels = await Task.Run(() => Context.PlanetChatChannels.Where(x => x.Parent_Id == id).ToList()); //Check if any channels in this category are the main channel foreach (PlanetChatChannel channel in channels) { if (channel.Id == planet.Main_Channel_Id) { return(new TaskResult(false, "You can not delete your main channel!")); } } //If not, then delete the channels foreach (PlanetChatChannel channel in channels) { Context.PlanetChatChannels.Remove(channel); } foreach (PlanetCategory Category in categories) { Category.Parent_Id = null; } ulong parentId = category.Planet_Id; Context.PlanetCategories.Remove(category); await Context.SaveChangesAsync(); await PlanetHub.Current.Clients.Group($"p-{parentId}").SendAsync("RefreshChannelList", ""); return(new TaskResult(true, "Successfully deleted.")); }