Ejemplo n.º 1
0
    protected void cmdSave_OnClick(object sender, EventArgs e)
    {
        WebServiceResponse rsp;
        DBPerson           user;
        bool created = false;

        if (response == null)
        {
            user       = new DBPerson();
            user.login = txtUserName.Text;
            created    = true;
        }
        else
        {
            user = response.User;
        }
        user.fullname      = txtFullName.Text;
        user.password      = txtPassword.Text;
        user.roles         = txtRoles.Text;
        user.irc_nicknames = txtIRCNicks.Text;
        rsp = Utils.LocalWebService.EditUser(Master.WebServiceLogin, user);
        if (rsp.Exception != null)
        {
            lblMessage.Text = rsp.Exception.Message;
        }
        else
        {
            if (!Authentication.IsLoggedIn(rsp) && created)
            {
                Authentication.Login(user.login, user.password, Request, Response);
            }
            Response.Redirect("User.aspx?username=" + HttpUtility.UrlEncode(user.login), false);
        }
    }
Ejemplo n.º 2
0
 public static void AddEmail(this DBPerson person, DB db, string email)
 {
     using (IDbCommand cmd = db.CreateCommand()) {
         cmd.CommandText = "INSERT INTO UserEmail (person_id, email) VALUES (@person_id, @email);";
         DB.CreateParameter(cmd, "person_id", person.id);
         DB.CreateParameter(cmd, "email", email);
         cmd.ExecuteNonQuery();
     }
 }
Ejemplo n.º 3
0
 public static void RemoveEmail(this DBPerson person, DB db, string email)
 {
     using (IDbCommand cmd = db.CreateCommand()) {
         cmd.CommandText = "DELETE FROM UserEmail WHERE person_id = @person_id AND email = @email;";
         DB.CreateParameter(cmd, "person_id", person.id);
         DB.CreateParameter(cmd, "email", email);
         cmd.ExecuteNonQuery();
     }
 }
Ejemplo n.º 4
0
        public static void LoginDB(DB db, LoginResponse response, string username, string roles, string ip4)
        {
            // We now create an account with an empty password and the specified roles.
            // Note that it is not possible to log into an account with an empty password
            // using the normal login procedure.

            DBPerson open_person = null;

            using (IDbCommand cmd = db.CreateCommand()) {
                cmd.CommandText = @"SELECT * FROM Person WHERE login = @login;";
                DB.CreateParameter(cmd, "login", username);
                using (var reader = cmd.ExecuteReader()) {
                    if (reader.Read())
                    {
                        open_person = new DBPerson(reader);
                    }
                }
            }

            if (open_person == null)
            {
                open_person       = new DBPerson();
                open_person.login = username;
                open_person.roles = roles;
                open_person.Save(db);
            }
            else
            {
                // only save if something has changed
                if (open_person.roles != roles)
                {
                    open_person.roles = roles;
                    open_person.Save(db);
                }
            }
            WebServiceLogin login = new WebServiceLogin();

            login.Ip4  = ip4;
            login.User = open_person.login;
            db.Audit(login, "DBLogin_Extensions.Login (username: {0}, ip4: {1})", username, ip4);

            var result = new DBLogin();

            result.person_id = open_person.id;
            result.ip4       = ip4;
            result.cookie    = CreateCookie();
            result.expires   = DateTime.Now.AddDays(1);
            result.Save(db);

            response.User      = username;
            response.UserName  = username;
            response.UserRoles = open_person.Roles;
            response.Cookie    = result.cookie;
        }
Ejemplo n.º 5
0
        private void FindPerson(DBPerson person, List <DBPerson> people)
        {
            using (DB db = new DB()) {
                using (IDbCommand cmd = db.CreateCommand()) {
                    cmd.CommandText = string.Empty;

                    // find registered people with the same email
                    if (person.Emails != null)
                    {
                        int email_counter = 0;
                        foreach (string email in person.Emails)
                        {
                            if (string.IsNullOrEmpty(email))
                            {
                                continue;
                            }
                            email_counter++;
                            cmd.CommandText += "SELECT Person.* FROM Person INNER JOIN UserEmail ON Person.id = UserEmail.person_id WHERE UserEmail.email ILIKE @email" + email_counter.ToString() + ";\n";
                            DB.CreateParameter(cmd, "email" + email_counter.ToString(), email);
                        }
                    }

                    // find registered people with the same fullname
                    if (!string.IsNullOrEmpty(person.fullname))
                    {
                        cmd.CommandText += "SELECT Person.* FROM Person WHERE fullname ILIKE @fullname;";
                        DB.CreateParameter(cmd, "fullname", person.fullname);
                    }

                    using (IDataReader reader = cmd.ExecuteReader()) {
                        do
                        {
                            while (reader.Read())
                            {
                                DBPerson guy = new DBPerson(reader);
                                if (people.Exists((v) => v.id == guy.id))
                                {
                                    continue;
                                }
                                people.Add(guy);
                            }
                        } while (reader.NextResult());
                    }
                }
            }

            if (people.Count == 0)
            {
                people.Add(person);
            }
        }
Ejemplo n.º 6
0
        public static List <string> GetEmails(this DBPerson person, DB db)
        {
            List <string> result = new List <string> ();

            using (IDbCommand cmd = db.CreateCommand()) {
                cmd.CommandText = "SELECT email FROM UserEmail WHERE person_id = @person_id;";
                DB.CreateParameter(cmd, "person_id", person.id);
                using (IDataReader reader = cmd.ExecuteReader()) {
                    while (reader.Read())
                    {
                        result.Add(reader.GetString(0));
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 7
0
 public VMPerson()
 {
     DeleteStundenCommand  = new MyICommand(DeleteStundenCommand_Click, CanStundenDelete);
     EditStundenCommand    = new MyICommand(EditStundenCommand_Click, CanStundenEdit);
     AddStundenCommand     = new MyICommand(AddStundenCommand_Click, CanEdit);
     CreatePDFCommand      = new MyICommand(CreatePDFCommand_Click, CanEdit);
     CreatePDFMonatCommand = new MyICommand(CreatePDFMonatCommand_Click, CanEdit);
     DeleteCommand         = new MyICommand(DeleteCommand_Click, CanDelete);
     EditCommand           = new MyICommand(EditCommand_Click, CanEdit);
     AddCommand            = new MyICommand(AddCommand_Click);
     Value           = new SortableBindingList <MPerson>();
     ValueStunden    = new SortableBindingList <MStunden>();
     Selected        = null;
     SelectedStunden = null;
     _DB             = new DBPerson();
     Load();
 }
        public static Person DBToModel(DBPerson db)
        {
            Person person = new Person();

            person.ID          = db.ID;;
            person.Name        = db.Name;
            person.Age         = db.Age;
            person.Birthplace  = db.Birthplace;
            person.IsFemale    = db.IsFemale;
            person.Score       = db.Score;
            person.StuNum      = db.StuNum;
            person.Tel         = db.Tel;
            person.Hourlypay   = db.Hourlypay;
            person.HadDiscount = db.HadDiscount;
            person.CompanyList = new List <Company>();

            return(person);
        }
        public static DBPerson ModelToDB(Person person)
        {
            DBPerson db = new DBPerson();

            db.ID          = person.ID;
            db.Name        = person.Name;
            db.Age         = person.Age;
            db.Birthplace  = person.Birthplace;
            db.IsFemale    = person.IsFemale;
            db.Score       = person.Score;
            db.StuNum      = person.StuNum;
            db.Tel         = person.Tel;
            db.Hourlypay   = person.Hourlypay;
            db.HadDiscount = person.HadDiscount;


            return(db);
        }
Ejemplo n.º 10
0
        private void MenuItemAddPerson_Click(object sender, RoutedEventArgs e)
        {
            //var list = SQLiteHelper.GetTableBySQL("select * from Persons");

            PersonEditWnd wnd = new PersonEditWnd();

            wnd.Owner = this;
            if (wnd.ShowDialog() ?? false)
            {
                //点击了确认
                var      p  = wnd.Person;
                DBPerson db = ModelConvertHelper.ModelToDB(p);
                DbHelper.PersonBLL.Add(db);
                VM.PersonList.Add(p);
            }
            else
            {
                //点击了取消
            }
        }
Ejemplo n.º 11
0
        private static void Main(string[] args)
        {
            Person person1 = new Person();
            person1.Username = "******";
            person1.Password = "******";
            person1.Age = 25;
            DBPerson dbPerson = new DBPerson();
            dbPerson.Insert(person1);
            Console.WriteLine("新增了一条记录");

            Person person2 = dbPerson.GetById(4);
            person2.Password = "******";
            person2.Username = "******";
            person2.Age = 40;
            dbPerson.Update(person2);
            Console.WriteLine("Id为 4的记录已被更新");

            int id = 3;
            dbPerson.RemoveById(id);
            Console.WriteLine("Id为 3的记录已被删除");
        }
Ejemplo n.º 12
0
        private void MenuItemEditPerson_Click(object sender, RoutedEventArgs e)
        {
            var p = VM.SelectedPerson;

            PersonEditWnd wnd = new PersonEditWnd();

            wnd.Owner  = this;
            wnd.Person = (Person)p.Copy();
            if (wnd.ShowDialog() ?? false)
            {
                //点击了确认

                int index = VM.PersonList.IndexOf(p);
                VM.PersonList[index] = p = wnd.Person;
                DBPerson db = ModelConvertHelper.ModelToDB(p);
                DbHelper.PersonBLL.Update(db);
            }
            else
            {
                //点击了取消
            }
        }
Ejemplo n.º 13
0
 public Result GetAll(Request req)
 {
     return(DBPerson.GetAll());
 }
Ejemplo n.º 14
0
        public Result Add(Request req)
        {
            dynamic data = JsonConvert.DeserializeObject(req.Data.ToString());

            return(DBPerson.Add(data?.Name?.ToString(), data?.Position?.ToString()));
        }
Ejemplo n.º 15
0
        public static void Authenticate(string user_host_address, DB db, WebServiceLogin login, WebServiceResponse response, bool @readonly)
        {
            int         person_id;
            DBLoginView view = null;

            log.DebugFormat("WebService.Authenticate (Ip4: {0}, UserHostAddress: {1}, User: {2}, Cookie: {3}, Password: {4}", login == null ? null : login.Ip4, user_host_address, login == null ? null : login.User, login == null ? null : login.Cookie, login == null ? null : login.Password);

            // Check if credentials were passed in
            if (login == null || string.IsNullOrEmpty(login.User) || (string.IsNullOrEmpty(login.Password) && string.IsNullOrEmpty(login.Cookie)))
            {
                VerifyAnonymousAllowed();
                return;
            }

            string ip = !string.IsNullOrEmpty(login.Ip4) ? login.Ip4 : user_host_address;

            if (!string.IsNullOrEmpty(login.Password))
            {
                DBLogin result = DBLogin_Extensions.LoginUser(db, login.User, login.Password, ip, @readonly);
                if (result != null)
                {
                    if (@readonly)
                    {
                        person_id = result.person_id;
                    }
                    else
                    {
                        view = DBLoginView_Extensions.VerifyLogin(db, login.User, result.cookie, ip);
                        if (view == null)
                        {
                            log.Debug("Invalid cookie");
                            VerifyAnonymousAllowed();
                            return;
                        }
                        person_id = view.person_id;
                    }
                }
                else
                {
                    log.Debug("Invalid user/password");
                    VerifyAnonymousAllowed();
                    return;
                }
            }
            else
            {
                view = DBLoginView_Extensions.VerifyLogin(db, login.User, login.Cookie, ip);
                if (view == null)
                {
                    log.Debug("Invalid cookie");
                    VerifyAnonymousAllowed();
                    return;
                }
                person_id = view.person_id;
                log.DebugFormat("Verifying login, cookie: {0} user: {1} ip: {2}", login.Cookie, login.User, ip);
            }

            log.Debug("Valid credentials");

            if (response == null)
            {
                return;
            }

            DBPerson      person         = DBPerson_Extensions.Create(db, person_id);
            LoginResponse login_response = response as LoginResponse;

            if (login_response != null)
            {
                login_response.Cookie   = view != null ? view.cookie : null;
                login_response.FullName = person.fullname;
                login_response.ID       = person_id;
            }

            response.UserName  = person.login;
            response.UserRoles = person.Roles;
            log.DebugFormat("Authenticate2 Roles are: {0}", response.UserRoles == null ? "null" : string.Join(";", response.UserRoles));
        }
Ejemplo n.º 16
0
        public static void FindPeopleForCommit(DBLane lane, DBRevision revision, List <DBPerson> people)
        {
            DBPerson person;

            try {
                foreach (string repository in lane.repository.Split(new char [] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    string cache_dir = Configuration.GetSchedulerRepositoryCacheDirectory(repository);

                    if (!Directory.Exists(cache_dir))
                    {
                        continue;
                    }

                    using (Process git = new Process()) {
                        DateTime git_start = DateTime.Now;
                        git.StartInfo.FileName               = "git";
                        git.StartInfo.Arguments              = "log -1 --pretty=format:'%aE%n%aN%n%cE%n%cN' " + revision.revision;
                        git.StartInfo.WorkingDirectory       = cache_dir;
                        git.StartInfo.UseShellExecute        = false;
                        git.StartInfo.RedirectStandardOutput = true;

                        git.Start();

                        string author_email    = git.StandardOutput.ReadLine();
                        string author_name     = git.StandardOutput.ReadLine();
                        string committer_email = git.StandardOutput.ReadLine();
                        string committer_name  = git.StandardOutput.ReadLine();

                        // Wait 10 minutes for git to finish, otherwise abort.
                        if (!git.WaitForExit(1000 * 60 * 10))
                        {
                            Logger.Log("Getting commit info took more than 10 minutes, aborting.");
                            try {
                                git.Kill();
                                git.WaitForExit(10000);                                  // Give the process 10 more seconds to completely exit.
                            } catch (Exception ex) {
                                Logger.Log("Aborting commit info retrieval failed: {0}", ex.ToString());
                            }
                        }

                        if (git.HasExited && git.ExitCode == 0)
                        {
                            Logger.Log("Got commit info successfully in {0} seconds", (DateTime.Now - git_start).TotalSeconds);
                            person          = new DBPerson();
                            person.fullname = author_name;
                            person.Emails   = new string [] { author_email };
                            people.Add(person);
                            if (author_name != committer_name && !string.IsNullOrEmpty(committer_name))
                            {
                                person          = new DBPerson();
                                person.fullname = committer_name;
                                person.Emails   = new string [] { committer_email };
                                people.Add(person);
                            }
                            Logger.Log("Git commit info for {0}: author_name = {1} author_email: {2} committer_name: {3} committer_email: {4}", revision.revision, author_name, author_email, committer_name, committer_email);
                        }
                        else
                        {
                            Logger.Log("Didn't get commit info, HasExited: {0}, ExitCode: {1}", git.HasExited, git.HasExited ? git.ExitCode.ToString() : "N/A");
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.Log("Exception while trying to get commit info: {0}", ex.ToString());
            }
        }
Ejemplo n.º 17
0
        public static void LoginOpenId(DB db, LoginResponse response, string email, string ip4)
        {
            Logger.Log(2, "DBLogin.LoginOpenId ({0}, {1})", email, ip4);

            if (string.IsNullOrEmpty(Configuration.OpenIdProvider))
            {
                throw new Exception("No OpenId provider available");
            }

            if (string.IsNullOrEmpty(Configuration.OpenIdRoles))
            {
                throw new Exception("No OpenId roles specified");
            }

            if (string.IsNullOrEmpty(email))
            {
                throw new Exception("OpenId authentication requires an email");
            }

            string [] specs = Configuration.OpenIdRoles.Split(';');
            foreach (var spec in specs)
            {
                // email:role1,role2
                string [] split = spec.Split(':');
                if (split.Length != 2)
                {
                    Logger.Log("AuthenticateOpenId: Invalid role spec: {0}", spec);
                    continue;
                }

                if (string.IsNullOrEmpty(split [1]))
                {
                    Logger.Log("AuthenticateOpenId: No roles specified for {0}", split [0]);
                    continue;
                }

                if (!Regex.IsMatch(email, split [0]))
                {
                    continue;
                }

                // We now create an account with an empty password and the specified roles.
                // Note that it is not possible to log into an account with an empty password
                // using the normal login procedure.

                DBPerson open_person = null;

                using (IDbCommand cmd = db.CreateCommand()) {
                    cmd.CommandText = @"SELECT * FROM Person WHERE login = @login;";
                    DB.CreateParameter(cmd, "login", email);
                    using (var reader = cmd.ExecuteReader()) {
                        if (reader.Read())
                        {
                            open_person = new DBPerson(reader);
                        }
                    }
                }

                if (open_person == null)
                {
                    open_person       = new DBPerson();
                    open_person.login = email;
                    open_person.roles = split [1];
                    open_person.Save(db);
                }
                else
                {
                    // only save if something has changed
                    if (open_person.roles != split [1])
                    {
                        open_person.roles = split [1];
                        open_person.Save(db);
                    }
                }

                var result = new DBLogin();
                result.person_id = open_person.id;
                result.ip4       = ip4;
                result.cookie    = CreateCookie();
                result.expires   = DateTime.Now.AddDays(1);
                result.Save(db);

                response.User      = email;
                response.UserName  = email;
                response.UserRoles = open_person.Roles;
                response.Cookie    = result.cookie;

                return;
            }

            throw new Exception("The provided email address is not allowed to log in");
        }