Beispiel #1
0
        internal CheckResult New(string username, string password)
        {
            IMongoCollection <DBEntry.User> usersCollection = Program.Database.GetCollection <DBEntry.User>("users");
            List <DBEntry.User>             users           = usersCollection.Find(x => x.Username == username).ToList();

            if (users.Count > 0)
            {
                return(new CheckResult()
                {
                    authorized = false,
                    type = "unsuccessful",
                    message = "username already taken",
                    success = true
                });
            }
            DBEntry.User entry = new DBEntry.User()
            {
                Username           = username,
                Password           = HashPassword(password),
                Cookie             = GenerateKey(),
                CookieAvailability = DateTime.Now.AddDays(1),
                _id = MongoDB.Bson.ObjectId.GenerateNewId()
            };
            usersCollection.InsertOne(entry);
            return(new CheckResult()
            {
                authorized = true,
                type = "successful",
                message = "success",
                cookieKey = entry.Cookie,
                success = true
            });
        }
Beispiel #2
0
        internal Result Invite(string userKey, string communityId, string userToInvite)
        {
            IMongoCollection <DBEntry.Community> communitiesCollection = Program.Database.GetCollection <DBEntry.Community>("communities");
            IMongoCollection <DBEntry.User>      usersCollection       = Program.Database.GetCollection <DBEntry.User>("users");
            List <DBEntry.User> users = usersCollection.Find(x => x.Cookie == userKey).ToList();

            if (users.Count > 1)
            {
                return(new Api.Result()
                {
                    type = "unauthorized",
                    success = false,
                    authorized = false,
                    message = "invalid key"
                });
            }
            DBEntry.User user = users[0];
            if (user.CookieAvailability < DateTime.Now)
            {
                return(new Api.Result()
                {
                    type = "unauthorized", success = false, authorized = false, message = "key to old"
                });
            }
            List <DBEntry.Community> communities = communitiesCollection.Find(x => x.UniqueName == communityId).ToList();

            if (communities.Count != 1)
            {
                return(new Api.Result()
                {
                    type = "no community", authorized = true, success = false, message = "community does not exists"
                });
            }
            DBEntry.Community community = communities[0];
            if (!community.Users.Contains(user.Username))
            {
                return(new Api.Result()
                {
                    type = "unauthorized for community", authorized = true, success = false, message = "you are not part of that community"
                });
            }
            if (community.Users.Contains(userToInvite))
            {
                return(new Api.Result()
                {
                    type = "unauthorized for target user", authorized = true, success = false, message = "user is already in this community"
                });
            }
            List <string> userList = community.Users.ToList();

            userList.Add(userToInvite);
            community.Users = userList.ToArray();
            communitiesCollection.ReplaceOneAsync(x => x.UniqueName == community.UniqueName, community);
            return(new Api.Result()
            {
                type = "successful", authorized = true, success = true, message = "all good"
            });
        }
        internal ResultBase RenameBox(string userKey, string boxKey, string newName)
        {
            (UserGatewayController.CheckUserResult userResult, DBEntry.User user) = UserGatewayController.CheckUser(userKey);
            if (!userResult.success)
            {
                return(userResult);
            }
            IMongoCollection <DBEntry.User>      usersCollection       = Program.Database.GetCollection <DBEntry.User>("users");
            IMongoCollection <DBEntry.Community> communitiesCollection = Program.Database.GetCollection <DBEntry.Community>("communities");
            List <DBEntry.User>      boxesUser      = usersCollection.Find(x => x.Boxes.Any(y => y.Key == boxKey)).ToList();
            List <DBEntry.Community> boxesCommunity = communitiesCollection.Find(x => x.Boxes.Any(y => y.Key == boxKey)).ToList();

            if (boxesCommunity.Count + boxesUser.Count != 1)
            {
                return(new Result()
                {
                    authorized = true,
                    success = true,
                    type = "unsuccessful",
                    message = "nfc id does not exist"
                });
            }
            DBEntry.Box box = boxesCommunity.Select(x => x.Boxes.First(y => y.Key == boxKey)).Concat(boxesUser.Select(x => x.Boxes.First(y => y.Key == boxKey))).ElementAt(0);
            box.Name = newName;
            if (boxesUser.Select(x => x.Boxes.First(y => y.Key == box.Key)).Count() == 1)
            {
                DBEntry.User replaceUser = boxesUser.ElementAt(0);
                for (int i = 0; i < replaceUser.Boxes.Length; i++)
                {
                    if (replaceUser.Boxes[i].Key == box.Key)
                    {
                        replaceUser.Boxes[i] = box;
                    }
                }
                usersCollection.ReplaceOne(x => x.Username == replaceUser.Username, replaceUser);
            }
            else
            {
                DBEntry.Community replaceCommunity = boxesCommunity.ElementAt(0);
                for (int i = 0; i < replaceCommunity.Boxes.Length; i++)
                {
                    if (replaceCommunity.Boxes[i].Key == box.Key)
                    {
                        replaceCommunity.Boxes[i] = box;
                    }
                }
                communitiesCollection.ReplaceOne(x => x.UniqueName == replaceCommunity.UniqueName, replaceCommunity);
            }
            return(new Result()
            {
                authorized = true,
                message = "all good",
                success = true,
                type = "successful"
            });
        }
Beispiel #4
0
        internal ResultBase New(string userKey, string communityId, string communityName)
        {
            IMongoCollection <DBEntry.Community> communitiesCollection = Program.Database.GetCollection <DBEntry.Community>("communities");
            IMongoCollection <DBEntry.User>      usersCollection       = Program.Database.GetCollection <DBEntry.User>("users");
            List <DBEntry.User> users = usersCollection.Find(x => x.Cookie == userKey).ToList();

            if (users.Count > 1)
            {
                return(new Result()
                {
                    type = "unauthorized", success = false, authorized = false, message = "invalid key"
                });
            }
            DBEntry.User user = users[0];
            if (user.CookieAvailability < DateTime.Now)
            {
                return(new Result()
                {
                    type = "unauthorized", success = false, authorized = false, message = "key to old"
                });
            }
            DBEntry.Community entry = new DBEntry.Community()
            {
                Users      = new string[] { user.Username },
                Owner      = user.Username,
                Name       = communityName,
                UniqueName = communityId,
                _id        = MongoDB.Bson.ObjectId.GenerateNewId(),
            };
            if (communitiesCollection.Find(x => x.UniqueName == entry.UniqueName).ToList().Count > 0)
            {
                return(new Result()
                {
                    type = "unsuccessful", success = false, authorized = true, message = "community id already exists"
                });
            }
            communitiesCollection.InsertOne(entry);
            return(new Result()
            {
                type = "success", success = true, authorized = true, message = "all good"
            });
        }
        internal NewBoxResult NewBox(string nfcId, string location)
        {
            IMongoCollection <DBEntry.User> usersCollection = Program.Database.GetCollection <DBEntry.User>("users");
            List <DBEntry.User>             users           = usersCollection.Find(x => x.NFCid == nfcId).ToList();

            if (users.Count != 1)
            {
                return(new NewBoxResult()
                {
                    authorized = false,
                    type = "unauthorized",
                    message = "nfc id did not exist",
                    success = false,
                    key = null,
                    owner = null
                });
            }
            ObjectId objectid = ObjectId.GenerateNewId();

            DBEntry.User       user  = users[0];
            List <DBEntry.Box> boxes = user.Boxes.ToList();

            boxes.Add(new DBEntry.Box()
            {
                Key = objectid.ToString(), Location = location, Name = "unnamed", _id = objectid
            });
            user.Boxes = boxes.ToArray();
            usersCollection.ReplaceOne(x => x.Username == user.Username, user);
            return(new NewBoxResult {
                authorized = true,
                key = objectid.ToString(),
                message = "all good",
                owner = user.Username,
                success = true,
                type = "succesful"
            });
        }
Beispiel #6
0
        internal static (CheckUserResult Result, DBEntry.User User) CheckUser(string key)
        {
            IMongoCollection <DBEntry.User> usersCollection = Program.Database.GetCollection <DBEntry.User>("users");

            List <DBEntry.User> users = usersCollection.Find(x => x.Cookie == key).ToList();

            if (users.Count == 0)
            {
                return(new CheckUserResult()
                {
                    type = "unauthorized",
                    authorized = false,
                    message = "key not found",
                    success = false
                }, null);
            }
            DBEntry.User user = users[0];
            if (users[0].CookieAvailability < DateTime.Now)
            {
                return(new CheckUserResult()
                {
                    type = "unauthorized",
                    authorized = false,
                    message = "key to old",
                    success = false
                }, user);
            }
            return(new CheckUserResult()
            {
                type = "authorized",
                authorized = true,
                message = "authorized",
                username = users[0].Username,
                success = true
            }, user);
        }
        internal ResultBase CheckNfc(string nfcId, string boxKey)
        {
            IMongoCollection <DBEntry.User>      usersCollection       = Program.Database.GetCollection <DBEntry.User>("users");
            IMongoCollection <DBEntry.Community> communitiesCollection = Program.Database.GetCollection <DBEntry.Community>("communities");
            List <DBEntry.User> dbUsers = usersCollection.Find(x => x.NFCid == nfcId).ToList();

            if (dbUsers.Count != 1)
            {
                return(new Result()
                {
                    authorized = false,
                    message = "no one has that nfc id",
                    success = false,
                    type = "unauthorized",
                });
            }
            DBEntry.User dbUser = dbUsers[0];
            List <DBEntry.IBoxCarrier> boxCarriers = usersCollection.Find(
                x => x.Boxes.Any(y => y.Key == boxKey))
                                                     .ToList().ToList <DBEntry.IBoxCarrier>().Concat(
                communitiesCollection.Find(
                    x => x.Boxes.Any(y => y.Key == boxKey))
                .ToList().ToList <DBEntry.IBoxCarrier>()).ToList();

            if (boxCarriers.Count != 1)
            {
                return(new Result()
                {
                    authorized = true,
                    message = "key not found",
                    success = false,
                    type = "unsuccesful"
                });
            }
            DBEntry.IBoxCarrier boxCarrier = boxCarriers[0];
            if (boxCarrier is DBEntry.User user)
            {
                if (user.Username == dbUser.Username)
                {
                    return(new Result()
                    {
                        authorized = true,
                        message = "all good",
                        success = true,
                        type = "succesful"
                    });
                }
                else
                {
                    return(new Result()
                    {
                        authorized = true,
                        message = "the user is not the owner of this box",
                        success = false,
                        type = "unsuccesful"
                    });
                }
            }
            else if (boxCarrier is DBEntry.Community community)
            {
                if (community.Users.Contains(dbUser.Username))
                {
                    return(new Result()
                    {
                        authorized = true,
                        message = "all good",
                        success = true,
                        type = "succesful"
                    });
                }
                else
                {
                    return(new Result()
                    {
                        authorized = true,
                        message = "the user is not part of this box's community",
                        success = false,
                        type = "unsuccesful"
                    });
                }
            }
            else
            {
                return(new Result()
                {
                    authorized = true,
                    message = "error in IBoxCarrier type handling",
                    success = false,
                    type = "unsuccesful"
                });
            }
        }
        internal ResultBase MoveBox(string userKey, string boxKey, string to, string toType)
        {
            if (!(toType == "community" || toType == "user"))
            {
                return(new Result()
                {
                    authorized = false, success = false, type = "unsuccessful", message = "fromType or toType is not valid"
                });
            }
            (UserGatewayController.CheckUserResult userCheck, DBEntry.User dbUser) = UserGatewayController.CheckUser(userKey);
            if (!userCheck.authorized)
            {
                return(userCheck);
            }
            IMongoCollection <DBEntry.User>      usersCollection       = Program.Database.GetCollection <DBEntry.User>("users");
            IMongoCollection <DBEntry.Community> communitiesCollection = Program.Database.GetCollection <DBEntry.Community>("communities");
            List <DBEntry.User>      boxesUser      = usersCollection.Find(x => x.Boxes.Any(y => y.Key == boxKey)).ToList();
            List <DBEntry.Community> boxesCommunity = communitiesCollection.Find(x => x.Boxes.Any(y => y.Key == boxKey)).ToList();

            if (boxesCommunity.Count + boxesUser.Count != 1)
            {
                return(new Result()
                {
                    authorized = true,
                    success = true,
                    type = "unsuccessful",
                    message = "box does not exist"
                });
            }
            DBEntry.Box box = boxesCommunity.Select(x => x.Boxes.First(y => y.Key == boxKey)).Concat(boxesUser.Select(x => x.Boxes.First(y => y.Key == boxKey))).ElementAt(0);
            if (boxesCommunity.Count == 1)
            {
                DBEntry.Community  community = boxesCommunity[0];
                List <DBEntry.Box> boxes     = community.Boxes.ToList();
                boxes.Remove(box);
                community.Boxes = boxes.ToArray();
                communitiesCollection.ReplaceOne(x => x.UniqueName == community.UniqueName, community);
            }
            else
            {
                DBEntry.User       user  = boxesUser[0];
                List <DBEntry.Box> boxes = user.Boxes.ToList();
                boxes.Remove(box);
                user.Boxes = boxes.ToArray();
                usersCollection.ReplaceOne(x => x.Username == user.Username, user);
            }
            if (toType == "community")
            {
                List <DBEntry.Community> communities = communitiesCollection.Find(x => x.UniqueName == to).ToList();
                if (communities.Count != 1)
                {
                    return(new Result()
                    {
                        authorized = true,
                        success = false,
                        type = "unsuccessful",
                        message = "to does not exist"
                    });
                }
                DBEntry.Community  community = communities[0];
                List <DBEntry.Box> boxes     = community.Boxes.ToList();
                boxes.Add(box);
                community.Boxes = boxes.ToArray();
                communitiesCollection.ReplaceOne(x => x.UniqueName == community.UniqueName, community);
            }
            else
            {
                List <DBEntry.User> users = usersCollection.Find(x => x.Username == to).ToList();
                if (users.Count != 1)
                {
                    return(new Result()
                    {
                        authorized = true,
                        success = false,
                        type = "unsuccessful",
                        message = "to does not exist"
                    });
                }
                DBEntry.User       user  = users[0];
                List <DBEntry.Box> boxes = user.Boxes.ToList();
                boxes.Add(box);
                user.Boxes = boxes.ToArray();
                usersCollection.ReplaceOne(x => x.Username == user.Username, user);
            }
            return(new Result()
            {
                authorized = true,
                success = true,
                type = "successful",
                message = "all good",
            });
        }