Exemple #1
0
        public LockModel CreateLock(string lockName, IList <int> allowedUsers)
        {
            using (var smartLock = new SmartLockEntities())
            {
                int changes = 0;

                var lockInfo = new LockInfo
                {
                    Name  = lockName,
                    State = "Locked"
                };

                smartLock.LockInfoes.Add(lockInfo);
                changes += smartLock.SaveChanges();

                if (allowedUsers.Count > 0)
                {
                    smartLock.LockAccesses.AddRange(this.PopulateLockAccessList(lockInfo.LockId, allowedUsers));
                    changes += smartLock.SaveChanges();
                }

                if (changes == allowedUsers.Count + 1)
                {
                    return(new LockModel
                    {
                        LockId = lockInfo.LockId,
                        Name = lockName,
                        State = lockInfo.State,
                        AllowedUsers = allowedUsers
                    });
                }

                return(null);
            }
        }
Exemple #2
0
        public bool ModifyLockState(int lockId, int userId, LockState state)
        {
            using (var smartLock = new SmartLockEntities())
            {
                LockInfo lockInfo = smartLock.LockInfoes.FirstOrDefault(l => l.LockId == lockId);
                if (lockInfo == null)
                {
                    // lock not found.
                    throw new LockNotFoundException("Lock not found.");
                }

                LockAccess lockAccess =
                    smartLock.LockAccesses.FirstOrDefault(l => l.UserId == userId && l.LockId == lockId);

                // TODO: Expand check to limit access based on time
                if (lockAccess == null)
                {
                    // user does not have access to the lock. Throw exception.
                    throw new UnauthorizedUserException("User unauthorized.");
                }

                // modify lock state.
                lockInfo.State = String.Concat(state.ToString(), "ed");

                int changes = smartLock.SaveChanges();
                return(changes == 1 ? true : false);
            }
        }
Exemple #3
0
        public bool CreateUserAccess(int lockId, IList <int> allowedUsers)
        {
            using (var smartLock = new SmartLockEntities())
            {
                smartLock.LockAccesses.AddRange(this.PopulateLockAccessList(lockId, allowedUsers));
                int changes = smartLock.SaveChanges();

                return(changes == allowedUsers.Count ? true : false);
            }
        }
Exemple #4
0
        public bool CreateEvent(int lockId, int userId, string lockState)
        {
            using (var smartLock = new SmartLockEntities())
            {
                smartLock.UserEvents.Add(
                    new UserEvent
                {
                    LockId    = lockId,
                    UserId    = userId,
                    State     = lockState,
                    Timestamp = DateTime.UtcNow
                });

                int change = smartLock.SaveChanges();
                return(change == 1 ? true : false);
            }
        }
Exemple #5
0
        public int CreateUser(string userName, string userEmail, string userPassword, bool isAdmin)
        {
            var hashedPassword = this.GetSHA256Password(userPassword);

            using (var smartLock = new SmartLockEntities())
            {
                var userInfo = new UserInfo
                {
                    UserName = userName,
                    Email    = userEmail,
                    Password = hashedPassword,
                    IsAdmin  = isAdmin
                };

                smartLock.UserInfoes.Add(userInfo);
                smartLock.SaveChanges();

                return(userInfo.UserId);
            }
        }