Commit 5f16d33f authored by Dorchies David's avatar Dorchies David
Browse files

feat: retriieving cookie from username/password

- Log via INRA SSO - OK
- Browse the temps-activite website to pointage page - OK
- Post form for one date - not working yet

Refs #1
parent 8e37d114
......@@ -12,6 +12,10 @@ Encoding: UTF-8
LazyData: true
Imports:
httr,
lubridate,
rvest,
stringr
RoxygenNote: 7.1.1
Roxygen: list(markdown = TRUE)
Depends:
magrittr
# Generated by roxygen2: do not edit by hand
export(add_temps)
export(get_user_id)
export(jump_to_mon_calendrier)
export(jump_to_pointage)
export(login)
export(set_cookie)
importFrom(httr,POST)
importFrom(httr,set_cookies)
......
......@@ -7,7 +7,7 @@
#' @param duree Duration of the working day
#' @param type_regulation Regularisation type (default 22 for "Heures normales")
#' @param commentaire [character] Comment for the regularisation
#' @param cfg network configuration
#' @param cfg network configuration using config package facilities (package configuration by default)
#'
#' @details
#' If `cookies` is provided in a single [character] format, [set_cookie] is automatically called.
......@@ -19,39 +19,33 @@
#' @examples
#' \dontrun{
#' library(hatata)
#' login("pnom", "my_password")
#' add_temps(
#' cookies = "Cookie: SERVERID83378=6df7a8ae|YJJSU|YJJNe; PHPSESSID=nvda6b0h1; OSCONTROL=u3bmeg9",
#' user_id = "4242",
#' date = "15/04/2021"
#' )
#' }
add_temps <- function(
cookies,
user_id,
session,
date,
duree = "+07h44",
type_regulation = "22",
commentaire = "",
user_id = get_user_id(session),
cfg = config::get(file = system.file("config.yml", package = "hatata"))
) {
url_add <- paste(cfg$url, cfg$param_ass, sep = "?")
if(!inherits(cookies, "HatataCookie")) {
cookies <- set_cookie(cookies)
if(!inherits(session, "hatata_pointage")) {
session <- jump_to_pointage(session)
}
POST(
url_add,
user_agent(cfg$user_agent),
set_cookies(.cookies = cookies),
body = list(
date = date,
type_regulation = type_regulation,
commentaire = commentaire,
user_id = user_id,
id_form = cfg$form_add,
duree = duree
)
)
rs <- rvest::read_html(system.file("form_add.html", package = "hatata"))
rf <- rvest::html_form(rs)[[1]]
# Define proper url for form action
rf$action <- paste(cfg$url_post_add, cfg$param_add, sep = "?")
# Set form fields
rf <- rf %>% rvest::html_form_set(date = date,
type_regulation = type_regulation,
commentaire = commentaire,
user_id = user_id,
duree = duree)
rvest::session_submit(session, rf)
}
#' Get user_id from html session
#'
#' The session must have been logged and "jumped to time" page.
#'
#' @param session a [rvest::session] provided by [jump_to_mon_calendrier] function
#'
#' @return a [character] with the user id (ex: "123")
#' @export
#'
#' @examples
#' \dontrun{
#' library(hatata)
#' session <- login("pnom", "password") %>% jump_to_mon_calendrier()
#' get_user_id(session)
#' }
get_user_id <- function(session) {
if(!inherits(session, "hatata_mon_calendrier")) {
stop("`session` must be computed by `jump_to_mon_calendrier` function")
}
stringr::str_match(session$response$url, "user_id=([0-9]+)")[1,2]
}
#' Lead session to the page "Mon calendrier"
#'
#' @param session a [rvest::session] provided by [login] function
#'
#' @return a [rvest::session] set to the page "Mon calendrier"
#' @export
#'
#' @examples
#' \dontrun{
#' library(hatata)
#' session <- login("pnom", "password") %>% jump_to_mon_calendrier()
#' }
jump_to_mon_calendrier <- function(session) {
if(!inherits(session, "hatata_logged")) {
stop("`session` must be created by `login` function")
}
session <- session %>% rvest::session_follow_link("Mes temps") %>%
rvest::session_follow_link("Mon calendrier")
class(session) <- c("hatata_mon_calendrier", class(session))
session
}
#' Jump to the pointage page of a given month
#'
#' @param session a [rvest::session] provided by [login] or [jump_to_mon_calendrier] functions
#' @param date a [character] representing the current date in format "%Y-%m-%d"
#' @param user_id the user_id get from the website (automatically retrieved by default)
#' @param cfg network configuration using config package facilities (package configuration by default)
#'
#' @return
#' @export
#'
#' @examples
#' \dontrun{
#' library(hatata)
#' session <- login("pnom", "password") %>% jump_to_pointage()
#' }
jump_to_pointage <- function(session,
date = format(Sys.Date(), "%Y-%m-%d"),
user_id = get_user_id(session),
cfg = config::get(file = system.file("config.yml", package = "hatata"))) {
if(!inherits(session, "hatata_mon_calendrier")) {
session <- jump_to_mon_calendrier(session)
}
date <- as.Date(date)
firstMonth <- format(lubridate::floor_date(date, "month"), "%Y-%m-%d")
lastMonth <- format(lubridate::ceiling_date(date, "month") - 1, "%Y-%m-%d")
url <- sprintf(cfg$url_pointage, firstMonth, lastMonth, user_id)
session <- session %>% rvest::session_jump_to(url)
class(session) <- c("hatata_pointage", class(session))
attr(session, "user_id") <- user_id
session
}
#' Login to the service
#'
#' @param username [character] username at INRAE
#' @param password [character] password
#' @param cfg network configuration using config package facilities
#'
#' @return a [rvest::session] object logged to the page https://temps-activites.inrae.fr/fr/
#' @export
#'
#' @examples
#' \dontrun{
#' library(hatata)
#' login("pnom", "myPassword")
#' }
login <- function(username,
password,
cfg = config::get(file = system.file("config.yml", package = "hatata"))) {
rs <- rvest::session(cfg$url_temps_activites)
rf <- rvest::html_form(rs)[[1]]
rf <- rvest::html_form_set(rf, username = username, password = password)
rs <- rvest::session_submit(rs, rf, submit = cfg$idp_id_submit)
rf2 <- rvest::html_form(rs)[[1]]
rs <- rvest::session_submit(rs, rf2)
class(rs) <- c("hatata_logged", class(rs))
rs
}
default:
form_add: form_add_regul
url: https://temps-activites.inra.fr/temps/index.php
url_param_add: controller=Regulation&action=add
url_temps_activites: https://temps-activites.inrae.fr/fr/
url_pointage: https://temps-activites.inra.fr/temps/index.php?controller=Pointage/Feuille&action=showTempsTheorique&date_debut=%s&date_fin=%s&user_id=%s
url_post_add: https://temps-activites.inrae.fr/temps/index.php
param_add: controller=Regulation&action=add
user_agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0
headers:
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1
headers_login:
Content-Type: application/x-www-form-urlencoded
Origin: https://idp.inra.fr
Referer: https://idp.inra.fr/cas/login
<html>
<body>
<div class="container-fluid">
<form method="post" action="" class="form-horizontal" id="form_add_regul">
<div class="row-fluid">
<div class="span12" style="text-align:center;">
Evènement pour le pointage du 01/07/2021 <input type="hidden" id="date" name="date" value="01/07/2021" data-old-value="01/07/2021">
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div class="control-group">
<label class="control-label" for="type_regulation">Type d'évènement</label>
<div class="controls">
<select name="type_regulation" id="type_regulation">
<option value="40" selected="selected">Décharge syndicale</option>
<option value="39">Don de sang et de moelle osseuse </option>
<option value="38">Examens médicaux de prévention + expertises médicales diligentées par l'employeur</option>
<option value="35">Examens médicaux obligatoires antérieurs et postérieurs à l'accouchement</option>
<option value="37">FACILITES DE SERVICE en cas d'allaitement</option>
<option value="33">Fonctions électives INRAE</option>
<option value="22">Heures normales</option>
<option value="36">REDUCTION DU TEMPS DE TRAVAIL (grossesse)</option>
<option value="34">Séances préparatoires à la naissance</option>
</select>
<br> <br> </div>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div class="control-group">
<label class="control-label" for="duree">Durée</label>
<div class="controls">
<input id="duree" class="duree time" type="text" style="width:5em" value="" name="duree" placeholder="+/-00h00">
</div>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div class="control-group">
<label class="control-label" for="commentaire">Commentaire</label>
<div class="controls">
<textarea style="height:5em;width:20em;" id="commentaire" name="commentaire"></textarea>
<br> </div>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span12" style="text-align:center;">
<input type="hidden" name="id_form" value="form_add_regul">
<input type="hidden" name="user_id" id="user_id" value="4242">
<input type="submit" value="Enregistrer" id="btn_enregistrer_regul" class="ui-button ui-widget ui-state-default ui-corner-all" role="button" aria-disabled="false">
</div>
</div>
</form>
</div>
</body>
</html>
\ No newline at end of file
......@@ -5,21 +5,16 @@
\title{Title}
\usage{
add_temps(
cookies,
user_id,
session,
date,
duree = "+07h44",
type_regulation = "22",
commentaire = "",
user_id = get_user_id(session),
cfg = config::get(file = system.file("config.yml", package = "hatata"))
)
}
\arguments{
\item{cookies}{Connection cookies in string format or key/value vector
(See \link{set_cookie})}
\item{user_id}{ID of the user}
\item{date}{Date to fill}
\item{duree}{Duration of the working day}
......@@ -28,7 +23,12 @@ add_temps(
\item{commentaire}{\link{character} Comment for the regularisation}
\item{cfg}{network configuration}
\item{user_id}{ID of the user}
\item{cfg}{network configuration using config package facilities (package configuration by default)}
\item{cookies}{Connection cookies in string format or key/value vector
(See \link{set_cookie})}
}
\value{
the return of the \link[httr:POST]{httr::POST} function
......@@ -42,8 +42,8 @@ If \code{cookies} is provided in a single \link{character} format, \link{set_coo
\examples{
\dontrun{
library(hatata)
login("pnom", "my_password")
add_temps(
cookies = "Cookie: SERVERID83378=6df7a8ae|YJJSU|YJJNe; PHPSESSID=nvda6b0h1; OSCONTROL=u3bmeg9",
user_id = "4242",
date = "15/04/2021"
)
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_user_id.R
\name{get_user_id}
\alias{get_user_id}
\title{Get user_id from html session}
\usage{
get_user_id(session)
}
\arguments{
\item{session}{a \link[rvest:session]{rvest::session} provided by \link{jump_to_mon_calendrier} function}
}
\value{
a \link{character} with the user id (ex: "123")
}
\description{
The session must have been logged and "jumped to time" page.
}
\examples{
\dontrun{
library(hatata)
session <- login("pnom", "password") \%>\% jump_to_mon_calendrier()
get_user_id(session)
}
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jump_to_mon_calendrier.R
\name{jump_to_mon_calendrier}
\alias{jump_to_mon_calendrier}
\title{Lead session to the page "Mon calendrier"}
\usage{
jump_to_mon_calendrier(session)
}
\arguments{
\item{session}{a \link[rvest:session]{rvest::session} provided by \link{login} function}
}
\value{
a \link[rvest:session]{rvest::session} set to the page "Mon calendrier"
}
\description{
Lead session to the page "Mon calendrier"
}
\examples{
\dontrun{
library(hatata)
session <- login("pnom", "password") \%>\% jump_to_mon_calendrier()
}
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jump_to_pointage.R
\name{jump_to_pointage}
\alias{jump_to_pointage}
\title{Jump to the pointage page of a given month}
\usage{
jump_to_pointage(
session,
date = format(Sys.Date(), "\%Y-\%m-\%d"),
user_id = get_user_id(session),
cfg = config::get(file = system.file("config.yml", package = "hatata"))
)
}
\arguments{
\item{session}{a \link[rvest:session]{rvest::session} provided by \link{login} or \link{jump_to_mon_calendrier} functions}
\item{date}{a \link{character} representing the current date in format "\%Y-\%m-\%d"}
\item{user_id}{the user_id get from the website (automatically retrieved by default)}
\item{cfg}{network configuration using config package facilities (package configuration by default)}
}
\value{
}
\description{
Jump to the pointage page of a given month
}
\examples{
\dontrun{
library(hatata)
session <- login("pnom", "password") \%>\% jump_to_pointage()
}
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/login.R
\name{login}
\alias{login}
\title{Login to the service}
\usage{
login(
username,
password,
cfg = config::get(file = system.file("config.yml", package = "hatata"))
)
}
\arguments{
\item{username}{\link{character} username at INRAE}
\item{password}{\link{character} password}
\item{cfg}{network configuration using config package facilities}
}
\value{
a \link[rvest:session]{rvest::session} object logged to the page https://temps-activites.inrae.fr/fr/
}
\description{
Login to the service
}
\examples{
\dontrun{
library(hatata)
login("pnom", "myPassword")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment