unity: how to grant and remove the authority of an object among several clients (or how to allow the client's controller to push a rigid body)

Just a small script to help anyone looking to pass authorization from one client to another when it hits an object.
I used it to let the player push a rigid body cube with his rigid body controller.

Made in Unity 2018.2

public class CubeAuthority: NetworkBehaviour {


Empty private OnCollisionEnter (Collision Collision)
{

if (collision.transform.tag == "Player" && isServer)
{
CmdAssignNetworkAuthority (GetComponent(), collision.transform.GetComponent());
}
}

[Command]
Public void CmdAssignNetworkAuthority (NetworkIdentity cubeId, NetworkIdentity clientId)
{
// Yes -> cube has an owner && owner is not the real owner
if (cubeId.clientAuthorityOwner! = null && cubeId.clientAuthorityOwner! = clientId.connectionToClient)
{
// remove authority
cubeId.RemoveClientAuthority (cubeId.clientAuthorityOwner);
}

// Yes -> cube has no owner
if (cubeId.clientAuthorityOwner == null)
{
// Add client as owner
cubeId.AssignClientAuthority (clientId.connectionToClient);
}
}
}

You have a good day.