Ejemplo n.º 1
0
        /// <summary>
        /// Attempt to login a user.
        /// </summary>
        /// <param name="username">The username of the user.</param>
        /// <param name="password">The password of the user.</param>
        /// <param name="person">The person object in which to fill in data.</param>
        /// <returns>The status of the login request.</returns>
        public LoginStatus Login(string username, string password, out Person person)
        {
            person = null;

            if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            {
                return LoginStatus.InvalidInput;
            }

            var p = Person.Login(username, password);

            if (p == null)
            {
                return LoginStatus.WrongUserNameOrPassword;
            }

            if (string.IsNullOrWhiteSpace(p.Token))
            {
                return LoginStatus.InvalidInput;
            }

            if (p.ID == -1)
            {
                return LoginStatus.CommunicationFailure;
            }

            person = p;
            return LoginStatus.Success;
        }
Ejemplo n.º 2
0
        public RequestStatus GetByToken(string token, out Person person)
        {
            person = null;

            if (string.IsNullOrWhiteSpace(token))
            {
                return RequestStatus.InvalidInput;
            }

            person = Person.GetByToken(token);

            return person == null ? RequestStatus.InvalidInput : RequestStatus.Success;
        }
Ejemplo n.º 3
0
        public RequestStatus GetByEmail(string token, ref Person person)
        {
            if (string.IsNullOrWhiteSpace(token) || person == null || string.IsNullOrWhiteSpace(person.Email))
            {
                return RequestStatus.InvalidInput;
            }

            person = Person.GetByEMail(person.Email);

            if (person == null)
            {
                return RequestStatus.InvalidInput;
            }

            person.Token = null;
            return RequestStatus.Success;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets all the bookings associated with a specific person.
        /// </summary>
        /// <param name="person">Person to get by.</param>
        /// <param name="bookings">The list of bookings that has the specified person.</param>
        /// <returns>The status of the request.</returns>
        public RequestStatus GetBookingsByPerson(Person person, out IEnumerable<Booking> bookings)
        {
            bookings = null;

            if (person == null)
            {
                return RequestStatus.InvalidInput;
            }

            bookings = Booking.GetAllBookings(person);
            return bookings != null ? RequestStatus.Success : RequestStatus.Error;
        }
Ejemplo n.º 5
0
        /// <summary>Logs the user into the system.</summary>
        /// <param name="username">The username to use in for login.</param>
        /// <param name="password">The password to use in for login.</param>
        /// <returns>The logged in person. Null if wrong username/password.</returns>
        public static Person Login(string username, string password)
        {
            if (username.Equals(Configuration.AdminEmail) && password.Equals(Configuration.AdminPassword))
            {
                var person = All.FirstOrDefault(p => p.Email.Equals(Configuration.AdminEmail));
                if (person != null)
                {
                    person.Token = GenerateToken();
                    BookITContext.Db.SaveChanges();
                    return person;
                }

                return null;
            }

            if (username.Equals(Configuration.TestEmail) && password.Equals(Configuration.TestPassword))
            {
                var person = All.FirstOrDefault(p => p.Email.Equals(Configuration.TestEmail));
                if (person != null)
                {
                    person.Token = GenerateToken();
                    BookITContext.Db.SaveChanges();

                    return person;
                }

                return null;
            }

            var ldap = new LdapConnection(Configuration.LDAPServer) { Credential = new NetworkCredential(username.Substring(0, username.IndexOf("@")), password) };
            try
            {
                ldap.Bind();
            }
            catch (COMException)
            {
                return new Person { ID = -1 };
            }
            catch (Exception e)
            {
                return e.Message.Contains("available") ? new Person { ID = -1 } : null;
            }

            var dsFilter = "(mail=" + username + ")";
            var de = new DirectoryEntry("LDAP://" + Configuration.LDAPServer, username.Substring(0, username.IndexOf("@")), password);
            var ds = new DirectorySearcher(de)
                        {
                            Filter = dsFilter
                        };

            SearchResult result = null;
            try
            {
                result = ds.FindOne();
            }
            catch (COMException)
            {
                return new Person { ID = -1 };
            }

            if (result == null)
            {
                return null;
            }

            if (!result.Properties.Contains("ituAffiliation") || !result.Properties.Contains("mail"))
            {
                return new Person { Email = string.Empty, Name = string.Empty, Token = string.Empty };
            }

            if (All.Any(p => p.Email.Equals(result.Properties["mail"][0].ToString())))
            {
                var person = All.First(p => p.Email.Equals(result.Properties["mail"][0].ToString()));
                person.Token = GenerateToken();
                BookITContext.Db.SaveChanges();
                return person;
            }
            else
            {
                var person = new Person
                    {
                        Email = result.Properties["mail"][0].ToString(),
                        Name = result.Properties["displayName"][0].ToString()
                    };
                person.Roles.Add(new Role { RoleName = result.Properties["ituAffiliation"][0].ToString() });
                person.Token = GenerateToken();
                BookITContext.Db.People.Add(person);
                BookITContext.Db.SaveChanges();
                return All.FirstOrDefault(p => p.Email.Equals(person.Email));
            }
        }
Ejemplo n.º 6
0
        public static IEnumerable<Booking> GetAllBookings(Person person)
        {
            if (person != null)
            {
                return All.Where(b => b.PersonID == person.ID);
            }

            return All;
        }