public IHttpActionResult PostCards(string externalId, [FromBody] CardsModel model)
        {
            try
            {
                UsersDataObject userDO = dataAccess.GetUserByExternalID(externalId);
                if (userDO == null)
                {
                    return(NotFound());
                }

                CardDataObject c = modelFactory.Parse(model);

                dataAccess.InsertUserCard(c.ExternalId, c.BadgeNumber, c.IsActive, c.PIN, userDO.Id);

                userDO = dataAccess.GetUserByExternalID(externalId);
                CardDataObject cardDO = userDO.Cards.Where(x => x.ExternalId == model.ExternalId).FirstOrDefault();
                if (cardDO == null)
                {
                    return(NotFound());
                }

                return(CreatedAtRoute <CardsModel>("Cards", new { externalId = externalId, cardExternalId = cardDO.ExternalId }, modelFactory.Create(externalId, cardDO)));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public IHttpActionResult DeleteCard(string externalId, string cardExternalId)
        {
            try
            {
                UsersDataObject userDO = dataAccess.GetUserByExternalID(externalId);
                if (userDO == null)
                {
                    return(NotFound());
                }

                CardDataObject cardDO = userDO.Cards.Where(x => x.ExternalId == cardExternalId).FirstOrDefault();
                if (cardDO == null)
                {
                    return(NotFound());
                }

                dataAccess.DeleteCardByExternalID(cardExternalId);

                return(new NoContentActionResult(Request.CreateResponse()));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public IHttpActionResult Patch(string externalId, [FromBody] Marvin.JsonPatch.JsonPatchDocument <UsersModel> userModelPatchDoc)
        {
            try
            {
                UsersDataObject userDO = dataAccess.GetUserByExternalID(externalId);
                if (userDO == null)
                {
                    return(NotFound());
                }

                UsersModel m = modelFactory.Create(userDO);

                if (userModelPatchDoc.Operations.Any(x => x.OperationType != Marvin.JsonPatch.Operations.OperationType.Replace ||
                                                     !(x.path == "name") ||
                                                     String.IsNullOrEmpty(x.value.ToString())))
                {
                    return(BadRequest("Patch only support op:replace path:name value:notnullorempty"));
                }

                userModelPatchDoc.ApplyTo(m);

                userDO = modelFactory.Parse(m);

                dataAccess.UpdateUser(userDO);

                return(Ok(m));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemple #4
0
        public UsersDataObject Parse(UsersModel userModel)
        {
            UsersDataObject obj = new UsersDataObject
            {
                ExternalID = userModel.ExternalID,
                Name       = userModel.Name
            };

            return(obj);
        }
Exemple #5
0
 public void UpdateUser(UsersDataObject userDO)
 {
     using (SqlConnection conn = this.GetConnection())
     {
         conn.Open();
         using (SqlCommand cmd = conn.CreateCommand())
         {
             cmd.CommandText = "UPDATE [dbo].[User] SET Name = @Name WHERE ExternalID = @ExternalID";
             cmd.Parameters.AddWithValue("@Name", userDO.Name);
             cmd.Parameters.AddWithValue("@ExternalID", userDO.ExternalID);
             cmd.ExecuteNonQuery();
         }
     }
 }
Exemple #6
0
 public UsersDataObject GetUserByExternalID(string externalID)
 {
     UsersDataObject user = null;
     using (SqlConnection connection = this.GetConnection())
     {
         using (var command = connection.CreateCommand())
         {
             command.CommandText = "GetUserByExternalID";
             command.CommandType = CommandType.StoredProcedure;
             command.Parameters.AddWithValue("@ExternalID", externalID);
             user = fillUserDataObj(command);
         }
     }
     return user;
 }
Exemple #7
0
        public UsersModel Create(UsersDataObject usersDO)
        {
            UsersModel model = new UsersModel
            {
                Url          = urlHelper.Link("User", new { externalId = usersDO.ExternalID }),
                ExternalID   = usersDO.ExternalID,
                Name         = usersDO.Name,
                IsActive     = usersDO.IsActive,
                UpdatedAt    = usersDO.UpdatedAt,
                CardsCount   = usersDO.CardsCount,
                FingersCount = usersDO.FingersCount
            };

            return(model);
        }
 public IHttpActionResult Get(string externalId)
 {
     try
     {
         UsersDataObject userDO = dataAccess.GetUserByExternalID(externalId);
         if (userDO == null)
         {
             return(NotFound());
         }
         return(Ok(modelFactory.Create(userDO)));
     }
     catch
     {
         return(InternalServerError());
     }
 }
        public IHttpActionResult GetCards(string externalId)
        {
            try
            {
                UsersDataObject userDO = dataAccess.GetUserByExternalID(externalId);
                if (userDO == null)
                {
                    return(NotFound());
                }

                return(Ok(userDO.Cards.Select(x => modelFactory.Create(externalId, x))));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public IHttpActionResult Delete(string externalId)
        {
            try
            {
                UsersDataObject userDO = dataAccess.GetUserByExternalID(externalId);
                if (userDO == null)
                {
                    return(NotFound());
                }

                dataAccess.DeleteUserByExternalID(externalId);
                return(new NoContentActionResult(Request.CreateResponse()));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemple #11
0
        public List<UsersDataObject> GetUserList(string filter)
        {

            long number1 = 0;
            bool canConvert = long.TryParse(filter.Trim(), out number1);

            string storProgName = canConvert ? "GetUserListByCard" : "GetUserListByName";
            var users = new List<UsersDataObject>();
            using (SqlConnection connection = this.GetConnection())
            {

                connection.Open();
                using (var command = new SqlCommand(storProgName, connection))
                {
                    //command.CommandText = storProgName;
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@Filter", filter);
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            int recCount = 0;
                            while (reader.Read() && !reader.IsClosed)
                            {
                                //if (recCount > 1000) break;
                                recCount++;
                                //var user =  GetUserByID(userID);
                                var user = new UsersDataObject();
                                user.Id = reader.GetInt32(reader.GetOrdinal("Id"));
                                user.Name = reader.GetString(reader.GetOrdinal("Name"));
                                user.ExternalID = reader.IsDBNull(reader.GetOrdinal("ExternalID")) ? "" : reader.GetString(reader.GetOrdinal("ExternalID"));
                                user.IsActive = reader.IsDBNull(reader.GetOrdinal("IsActive")) ? false : reader.GetBoolean(reader.GetOrdinal("IsActive"));
                                user.FingersCount = reader.IsDBNull(reader.GetOrdinal("Fingers")) ? 0 : reader.GetInt32(reader.GetOrdinal("Fingers"));
                                user.FacesCount = reader.IsDBNull(reader.GetOrdinal("Faces")) ? 0 : reader.GetInt32(reader.GetOrdinal("Faces"));
                                user.CardsCount = reader.IsDBNull(reader.GetOrdinal("Cards")) ? 0 : reader.GetInt32(reader.GetOrdinal("Cards"));
                                users.Add(user);

                            }
                        }
                    }
                }
            }
            return users;
        }
        public IHttpActionResult Post([FromBody] UsersModel model)
        {
            try
            {
                int id = dataAccess.InsertUser(model.ExternalID, model.Name, false);
                if (id == -1)
                {
                    return(BadRequest("Error saving user"));
                }

                UsersDataObject userDO = dataAccess.GetUserByID(id);

                return(CreatedAtRoute <UsersModel>("User", new { id = id }, modelFactory.Create(userDO)));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public IHttpActionResult Register(string externalId, [FromBody] UsersRegisterModel model)
        {
            try
            {
                UsersDataObject usersDO = dataAccess.GetUserByExternalID(externalId);
                if (usersDO == null)
                {
                    return(NotFound());
                }

                //var principal = Thread.CurrentPrincipal;
                ClaimsPrincipal user = Request.GetRequestContext().Principal as ClaimsPrincipal;
                if (!user.HasClaim(x => x.Type == ClaimTypes.Name))
                {
                    return(BadRequest("Invalid authentication, please request another access token"));
                }

                Claim username = user.Claims.Where(x => x.Type == ClaimTypes.Name).FirstOrDefault();
                Proxy.BioConnectAPI.SecurityTokenDto authorizedToken = bioConnectAPIProxy.RetrieveSecurityTokenByUsername(username.Value);
                if (authorizedToken == null)
                {
                    return(BadRequest("Invalid authentication, please request another access token"));
                }


                Proxy.BioConnectAPI.SecurityTokenDto newToken = new Proxy.BioConnectAPI.SecurityTokenDto
                {
                    Password  = model.Password,
                    UserName  = model.UserName,
                    UserID    = usersDO.Id,
                    Level     = model.Role,
                    Locations = new string[] { "Default" }
                };

                bioConnectAPIProxy.SaveSecurityToken(newToken, authorizedToken);
                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public IHttpActionResult Enroll(string externalId, [FromBody] UsersEnrollModel model)
        {
            try
            {
                UsersDataObject usersDO = dataAccess.GetUserByExternalID(externalId);
                if (usersDO == null)
                {
                    return(NotFound());
                }

                ClaimsPrincipal user = Request.GetRequestContext().Principal as ClaimsPrincipal;
                if (!user.HasClaim(x => x.Type == ClaimTypes.Name))
                {
                    return(BadRequest("Invalid authentication, please request another access token"));
                }

                Claim username = user.Claims.Where(x => x.Type == ClaimTypes.Name).FirstOrDefault();
                Proxy.BioConnectAPI.SecurityTokenDto authorizedToken = bioConnectAPIProxy.RetrieveSecurityTokenByUsername(username.Value);
                if (authorizedToken == null)
                {
                    return(BadRequest("Invalid authentication, please request another access token"));
                }

                EnrollDataModel dataModel = new EnrollDataModel
                {
                    readerID        = model.deviceID,
                    userDO          = usersDO,
                    enrollQuality   = model.enrollQuality,
                    fingerIndex     = model.fingerIndex,
                    callbackUrl     = model.callbackUrl,
                    authorizedToken = authorizedToken
                };

                enrollmentServer.Enqueue(dataModel);
                return(Content(System.Net.HttpStatusCode.Accepted, String.Format("Starting enrollment process for user: {0}", externalId)));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public IHttpActionResult PatchCard(string externalId, string cardExternalId, [FromBody] JsonPatchDocument <CardsModel> cardsModelPatchDoc)
        {
            try
            {
                UsersDataObject userDO = dataAccess.GetUserByExternalID(externalId);
                if (userDO == null)
                {
                    return(NotFound());
                }

                CardDataObject cardDO = userDO.Cards.Where(x => x.ExternalId == cardExternalId).FirstOrDefault();
                if (cardDO == null)
                {
                    return(NotFound());
                }

                CardsModel m = modelFactory.Create(externalId, cardDO);

                if (cardsModelPatchDoc.Operations.Any(x => x.OperationType != Marvin.JsonPatch.Operations.OperationType.Replace ||
                                                      !(x.path == "badgenumber" || x.path == "isactive" || x.path == "pin" || x.path == "priority" || x.path == "isbypass" || x.path == "facilitycode") ||
                                                      String.IsNullOrEmpty(x.value.ToString())))
                {
                    return(BadRequest("Patch only support op:replace path:badgenumber|isactive|pin value:notnullorempty"));
                }

                cardsModelPatchDoc.ApplyTo(m);

                cardDO = modelFactory.Parse(m);

                dataAccess.UpdateCard(cardDO);

                return(Ok(m));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public IHttpActionResult GetCards(string externalId, string cardExternalId)
        {
            try
            {
                UsersDataObject userDO = dataAccess.GetUserByExternalID(externalId);
                if (userDO == null)
                {
                    return(NotFound());
                }

                CardDataObject cardDO = userDO.Cards.Where(x => x.ExternalId == cardExternalId).FirstOrDefault();
                if (cardDO == null)
                {
                    return(NotFound());
                }

                return(Ok(modelFactory.Create(externalId, cardDO)));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemple #17
0
        private UsersDataObject fillUserDataObj(SqlCommand command)
        {
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            da.Fill(ds);

            if (ds.Tables[0].Rows.Count == 0) return null;

            var user = new UsersDataObject();

            DataRow userRow = ds.Tables[0].Rows[0];
            user.Id = Int32.Parse(userRow["Id"].ToString());
            user.ExternalID = userRow["ExternalID"].ToString();
            user.Name = userRow["Name"].ToString();
            user.DevicePassword = userRow["DevicePassword"].ToString();
            user.Password = userRow["Password"].ToString();
            user.ImageData = (byte[])(userRow["UserImage"] == DBNull.Value ? null : userRow["UserImage"]);
            user.IsAdministrator = userRow["IsAdministrator"] == DBNull.Value ? false : Convert.ToBoolean(userRow["IsAdministrator"].ToString());
            user.IsDeviceAdministrator = userRow["IsDeviceAdministrator"] == DBNull.Value ? false : Convert.ToBoolean(userRow["IsDeviceAdministrator"].ToString());
            user.UpdatedAt = userRow["UpdatedAt"] == DBNull.Value ? new DateTime(1970, 1, 1) : Convert.ToDateTime(userRow["UpdatedAt"]);
            user.IsActive = userRow["IsActive"] == DBNull.Value ? false : Convert.ToBoolean(userRow["IsActive"].ToString());
            user.Fingers = new ObservableCollection<FingerDataObject>();

            if (ds.Tables.Count > 1)
            {
                foreach (DataRow fingerRow in ds.Tables[1].Rows)
                {
                    var finger = new FingerDataObject();
                    int fingerInd = Int32.Parse(fingerRow["FingerIndex"].ToString());
                    finger.FingerIndex = FingerDataObject.FingerCodeDescriptions[(FingerDataObject.FingerCode)fingerInd];  // fingerRow["FingerIndex"].ToString();
                    finger.Templates = new ObservableCollection<TemplateObject>();
                    foreach (DataRow templateRow in ds.Tables[2].Rows)
                    {
                        int templateFingerInd = Int32.Parse(templateRow["FingerIndex"].ToString());

                        if (templateFingerInd == fingerInd)
                        {
                            var template = new TemplateObject();
                            template.Id = templateRow["Id"] == DBNull.Value ? 0 : Int32.Parse(templateRow["Id"].ToString());
                            template.QualityScore = Int32.Parse(templateRow["QualityScore"].ToString());
                            template.Checksum = Int64.Parse(templateRow["Checksum"].ToString());
                            template.FingerIndex = templateFingerInd;
                            template.UserID = Int32.Parse(templateRow["UserId"].ToString());
                            template.Data = (byte[])(templateRow["Data"] == DBNull.Value ? null : templateRow["Data"]);
                            template.UpdatedAt = templateRow["UpdatedAt"] == DBNull.Value ? new DateTime(1970, 1, 1) : Convert.ToDateTime(userRow["UpdatedAt"]);

                            finger.Templates.Add(template);
                        }
                    }
                    user.Fingers.Add(finger);
                }
                user.FingersCount = user.Fingers.Count;
            }

            user.Cards = new ObservableCollection<CardDataObject>();
            if (ds.Tables.Count > 3)
            {
                foreach (DataRow cardRow in ds.Tables[3].Rows)
                {
                    var card = new CardDataObject();
                    card.Id = cardRow["Id"].ToString();
                    card.ExternalId = cardRow["ExternalId1"].ToString();
                    card.IsActive = cardRow["IsActive"] == DBNull.Value ? false : Convert.ToBoolean(cardRow["IsActive"].ToString());
                    card.IsBypass = cardRow["IsBypass"] == DBNull.Value ? false : Convert.ToBoolean(cardRow["IsBypass"].ToString());
                    card.BadgeNumber = cardRow["BadgeNumber"].ToString();
                    card.Priority = cardRow["Priority"] == DBNull.Value ? 0 : Int32.Parse(cardRow["Priority"].ToString());
                    card.FacilityCode = cardRow["FacilityCode"] == DBNull.Value ? 0 : Int64.Parse(cardRow["FacilityCode"].ToString());
                    card.PIN = cardRow["PIN"].ToString();
                    user.Cards.Add(card);
                }
                user.CardsCount = user.Cards.Count;
            }

            user.Locations = new ObservableCollection<LocationDataObject>();
            if (ds.Tables.Count > 4)
                foreach (DataRow locationRow in ds.Tables[4].Rows)
                {
                    var location = new LocationDataObject();
                    location.Id = locationRow["Id"].ToString();
                    location.Location = locationRow["Location"].ToString();
                    user.Locations.Add(location);
                }
 

            user.Properties = new ObservableCollection<PropertyDataObject>();
            if (ds.Tables.Count > 5)
                foreach (DataRow propertyRow in ds.Tables[5].Rows)
                {
                    var Property = new PropertyDataObject();
                    Property.ID = propertyRow["User_id"].ToString();
                    Property.Property = new KeyValuePair<string, string>(propertyRow["propertyName"].ToString(), propertyRow["propertyValue"].ToString());
                    user.Properties.Add(Property);
                }

            return user;
        }