Exemplo n.º 1
0
        public EnrollFingersForm(GrantedUser user)
        {
            InitializeComponent();

            var serialNumbers = FingerprintReader.GetPluggedReadersSerialNumbers();

            if (serialNumbers.Count == 0)
            {
                MessageBox.Show("No fingerprint reader available.", "Module not Found!");
                Close();
                return;
            }

            enrollmentControl.ReaderSerialNumber = serialNumbers[0];
            _currentUser = user;

            labelUsername.Text = _currentUser.Login;

            foreach (var fp in _currentUser.Fingerprints)
            {
                enrollmentControl.EnrolledFingerMask += FingerIndexToMaskValue[(FingerIndex)fp.Index];
            }
        }
Exemplo n.º 2
0
        public async Task <string> AddOrUpdateUser(Stream streamdata)
        {
            try
            {
                StreamReader reader = new StreamReader(streamdata);
                string       res    = reader.ReadToEnd();
                reader.Close();
                reader.Dispose();

                JsonUser ju = JsonConvert.DeserializeObject <JsonUser>(res);
                if (ju != null)
                {
                    var ctx = await RemoteDatabase.GetDbContextAsync();

                    var user = ctx.GrantedUsers.GetByServerId(ju.ServerUserId);
                    if (user != null)     //update
                    {
                        if (ju.Password != null)
                        {
                            user.Password = PasswordHashing.Sha256Of(ju.Password);
                        }
                        user.FirstName        = ju.FirstName;
                        user.LastName         = ju.LastName;
                        user.BadgeNumber      = ju.BadgeNumber;
                        ctx.Entry(user).State = EntityState.Modified;
                        await ctx.SaveChangesAsync();

                        foreach (var dev in ctx.Devices)
                        {
                            ctx.GrantedAccesses.AddOrUpdateAccess(user, dev, ctx.GrantTypes.All());
                        }
                        await ctx.SaveChangesAsync();

                        ctx.Database.Connection.Close();
                        ctx.Dispose();
                        return("Success : " + user.ServerGrantedUserId);
                    }
                    else     //create
                    {
                        GrantedUser gu = new GrantedUser()
                        {
                            Login = ju.Login,
                            ServerGrantedUserId                          = ju.ServerUserId,
                            Password                                     = ju.Password != null?PasswordHashing.Sha256Of(ju.Password) : PasswordHashing.Sha256Of("123456"),
                                                             FirstName   = ju.FirstName,
                                                             LastName    = ju.LastName,
                                                             BadgeNumber = ju.BadgeNumber,
                                                             UserRank    = ctx.UserRanks.User(),
                        };
                        ctx.GrantedUsers.Add(gu);
                        await ctx.SaveChangesAsync();

                        foreach (var dev in ctx.Devices)
                        {
                            ctx.GrantedAccesses.AddOrUpdateAccess(gu, dev, ctx.GrantTypes.All());
                        }
                        await ctx.SaveChangesAsync();

                        ctx.Database.Connection.Close();
                        ctx.Dispose();
                        if (MyHostEvent != null)
                        {
                            MyHostEvent(this, new MyHostEventArgs("UpdateUserInfoList", null));
                        }
                        return("Success : " + gu.ServerGrantedUserId);
                    }
                }
                return("Error : Bad Parameters");
            }
            catch (Exception exp)
            {
                return("Exception : " + exp.InnerException + "-" + exp.Message);
            }
        }
        public static async Task <bool> GetAndStoreUserAsync()
        {
            try
            {
                string serverIP   = Properties.Settings.Default.ServerIp;
                int    serverPort = Properties.Settings.Default.ServerPort;

                //serverIP = "157.230.97.196";
                //serverPort = 3000;

                string urlServer = "http://" + serverIP + ":" + serverPort;
                var    client    = new RestClient(urlServer);
                client.Authenticator = new HttpBasicAuthenticator(publicApiLogin, publicApiMdp);
                var request = new RestRequest("/users", Method.GET);
                client.Timeout           = timeout;
                client.ReadWriteTimeout  = timeout;
                request.Timeout          = timeout;
                request.ReadWriteTimeout = timeout;

                var response = await client.ExecuteTaskAsync(request);

                if (response.IsSuccessful)
                {
                    // remove  granted standard users
                    var ctx = await RemoteDatabase.GetDbContextAsync();

                    /* ctx.GrantedAccesses.Clear();
                    *  await ctx.SaveChangesAsync();*/

                    //get device
                    Device mydev = ctx.Devices.GetBySerialNumber(Properties.Settings.Default.WallSerial);

                    if (mydev == null)
                    {
                        return(false);
                    }

                    var lstUser = JsonUserList.DeserializedJsonList(response.Content);
                    if ((lstUser != null) && (lstUser.Length > 0))
                    {
                        foreach (JsonUserList jsl in lstUser)
                        {
                            var original  = ctx.GrantedUsers.GetByServerId(jsl.user_id);
                            var original2 = ctx.GrantedUsers.GetByLogin(jsl.login);
                            if ((original != null) && (original.Login != "Admin"))
                            {
                                TimeSpan ts = jsl.updated_at - original.UpdateAt;
                                if (Math.Abs(ts.TotalSeconds) > 1)  // Not the latest but avoid ms
                                {
                                    original.ServerGrantedUserId = jsl.user_id;
                                    original.Login            = jsl.login;
                                    original.Password         = jsl.password;
                                    original.FirstName        = jsl.fname;
                                    original.LastName         = jsl.lname;
                                    original.BadgeNumber      = string.IsNullOrEmpty(jsl.badge_num) ? "000000" : jsl.badge_num;
                                    original.UserRankId       = 3;
                                    original.UpdateAt         = jsl.updated_at;
                                    ctx.Entry(original).State = EntityState.Modified;
                                    await ctx.SaveChangesAsync();

                                    //deletefingerprint for this user if exists

                                    var fpUser = ctx.Fingerprints.Where(gu => gu.GrantedUserId == original.GrantedUserId).ToList();
                                    if ((fpUser != null) && (fpUser.Count > 0))
                                    {
                                        foreach (SmartDrawerDatabase.DAL.Fingerprint fp in fpUser)
                                        {
                                            ctx.Fingerprints.Remove(fp);
                                        }
                                        await ctx.SaveChangesAsync();
                                    }

                                    if ((jsl.ftemplate != null) & (jsl.ftemplate.Count > 0))
                                    {
                                        for (int loop = 0; loop < jsl.ftemplate.Count; loop++)
                                        {
                                            if (!string.IsNullOrEmpty(jsl.ftemplate[loop]) && !string.IsNullOrEmpty(jsl.finger_index[loop]))
                                            {
                                                ctx.Fingerprints.Add(new SmartDrawerDatabase.DAL.Fingerprint
                                                {
                                                    GrantedUserId = original.GrantedUserId,
                                                    Index         = Convert.ToInt32(jsl.finger_index[loop]),
                                                    Template      = jsl.ftemplate[loop],
                                                });
                                            }
                                        }
                                        await ctx.SaveChangesAsync();
                                    }
                                    ctx.GrantedAccesses.AddOrUpdateAccess(original, mydev, ctx.GrantTypes.All());
                                    await ctx.SaveChangesAsync();
                                }
                            }
                            else if (original2 != null)
                            {
                                TimeSpan ts = jsl.updated_at - original2.UpdateAt;
                                if (Math.Abs(ts.TotalSeconds) > 1)  // Not the latest but avoid ms
                                {
                                    original2.ServerGrantedUserId = jsl.user_id;
                                    original2.Login            = jsl.login;
                                    original2.Password         = jsl.password;
                                    original2.FirstName        = jsl.fname;
                                    original2.LastName         = jsl.lname;
                                    original2.BadgeNumber      = string.IsNullOrEmpty(jsl.badge_num) ? "000000" : jsl.badge_num;
                                    original2.UserRankId       = 3;
                                    original2.UpdateAt         = jsl.updated_at;
                                    ctx.Entry(original2).State = EntityState.Modified;
                                    await ctx.SaveChangesAsync();

                                    //deletefingerprint for this user if exists

                                    var fpUser = ctx.Fingerprints.Where(gu => gu.GrantedUserId == original2.GrantedUserId).ToList();
                                    if ((fpUser != null) && (fpUser.Count > 0))
                                    {
                                        foreach (SmartDrawerDatabase.DAL.Fingerprint fp in fpUser)
                                        {
                                            ctx.Fingerprints.Remove(fp);
                                        }
                                        await ctx.SaveChangesAsync();
                                    }

                                    if ((jsl.ftemplate != null) & (jsl.ftemplate.Count > 0))
                                    {
                                        for (int loop = 0; loop < jsl.ftemplate.Count; loop++)
                                        {
                                            if (!string.IsNullOrEmpty(jsl.ftemplate[loop]) && !string.IsNullOrEmpty(jsl.finger_index[loop]))
                                            {
                                                ctx.Fingerprints.Add(new SmartDrawerDatabase.DAL.Fingerprint
                                                {
                                                    GrantedUserId = original2.GrantedUserId,
                                                    Index         = Convert.ToInt32(jsl.finger_index[loop]),
                                                    Template      = jsl.ftemplate[loop],
                                                });
                                            }
                                        }
                                        await ctx.SaveChangesAsync();
                                    }
                                    ctx.GrantedAccesses.AddOrUpdateAccess(original2, mydev, ctx.GrantTypes.All());
                                    await ctx.SaveChangesAsync();
                                }
                            }
                            else if ((original == null) && (original2 == null))
                            {
                                GrantedUser newUser = new GrantedUser()
                                {
                                    ServerGrantedUserId = jsl.user_id,
                                    Login       = jsl.login,
                                    Password    = jsl.password,
                                    FirstName   = jsl.fname,
                                    LastName    = jsl.lname,
                                    BadgeNumber = string.IsNullOrEmpty(jsl.badge_num) ? "000000" : jsl.badge_num,
                                    UpdateAt    = jsl.updated_at,
                                    UserRankId  = 3,
                                };
                                ctx.GrantedUsers.Add(newUser);
                                await ctx.SaveChangesAsync();

                                if ((jsl.ftemplate != null) & (jsl.ftemplate.Count > 0))
                                {
                                    for (int loop = 0; loop < jsl.ftemplate.Count; loop++)
                                    {
                                        if (!string.IsNullOrEmpty(jsl.ftemplate[loop]) && !string.IsNullOrEmpty(jsl.finger_index[loop]))
                                        {
                                            ctx.Fingerprints.Add(new SmartDrawerDatabase.DAL.Fingerprint
                                            {
                                                GrantedUserId = newUser.GrantedUserId,
                                                Index         = Convert.ToInt32(jsl.finger_index[loop]),
                                                Template      = jsl.ftemplate[loop],
                                            });
                                        }
                                    }
                                    await ctx.SaveChangesAsync();
                                }
                                ctx.GrantedAccesses.AddOrUpdateAccess(newUser, mydev, ctx.GrantTypes.All());
                                await ctx.SaveChangesAsync();
                            }
                        }
                        ctx.Database.Connection.Close();
                        ctx.Dispose();
                        return(true);
                    }
                    return(true);
                }
                return(false);
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
            catch (Exception error)
            {
                ExceptionMessageBox exp = new ExceptionMessageBox(error, "Error getting user");
                exp.ShowDialog();
                return(false);
            }
        }